硒和Lambda Chrome驱动程序问题

时间:2020-03-18 03:30:10

标签: selenium selenium-webdriver aws-lambda selenium-chromedriver

我一直在努力克服这个问题,但是没有任何效果。我在aws lambda中运行Nodejs10,下面是我的功能代码。

我一直收到以下错误:我尝试重新下载linux chromedriver,但是没有任何改变。

"Error: Server terminated early with status 126",
        "    at /var/task/node_modules/selenium-webdriver/remote/index.js:252:52",
        "    at processTicksAndRejections (internal/process/task_queues.js:94:5)",
        "From: Task: WebDriver.createSession()",
        "    at Function.createSession (/var/task/node_modules/selenium-webdriver/lib/webdriver.js:769:24)",
        "    at Function.createSession (/var/task/node_modules/selenium-webdriver/chrome.js:761:15)",
        "    at createDriver (/var/task/node_modules/selenium-webdriver/index.js:170:33)",
        "    at Builder.build (/var/task/node_modules/selenium-webdriver/index.js:642:16)",
        "    at Runtime.module.exports.haranalysis [as handler] (/var/task/handler.js:40:26)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"

功能代码:

'use strict';
process.env.FONTCONFIG_PATH = `${process.env.LAMBDA_TASK_ROOT}/fonts`;
if (process.env.LD_LIBRARY_PATH.startsWith("/var/task/lib:") !== true) {
  process.env.LD_LIBRARY_PATH = [new Set(["/var/task/lib", process.env.LD_LIBRARY_PATH.split(':')])].join(':');
};

module.exports.haranalysis = (event, context, callback) => {
    var webdriver = require('selenium-webdriver');
    var chrome = require('selenium-webdriver/chrome');
    // let chromedriver = require('chromedriver');
    require('chromedriver');
    // var path = require('chromedriver').path;
    // var service = new chrome.ServiceBuilder().build();
    // chrome.setDefaultService(service);
    var builder = new webdriver.Builder().forBrowser('chrome');
    var chromeOptions = new chrome.Options();
    var urlToTest = 'https://www.google.com.au';
    const defaultChromeFlags = [
        '--headless',
        '--disable-gpu',
        '--window-size=1280x1696', // Letter size
        '--no-sandbox',
        '--user-data-dir=/tmp/user-data',
        '--hide-scrollbars',
        '--enable-logging',
        '--log-level=0',
        '--v=99',
        '--single-process',
        '--data-path=/tmp/data-path',
        '--ignore-certificate-errors',
        '--homedir=/tmp',
        '--disk-cache-dir=/tmp/cache-dir'
    ];

    chromeOptions.setChromeBinaryPath("/var/task/lib/chrome");
    chromeOptions.addArguments(defaultChromeFlags);
    builder.setChromeOptions(chromeOptions);

    var driver = builder.build();
    driver.get(urlToTest);
    driver.getTitle().then(function(title) {

        console.log("Page title for " + urlToTest + " is " + title);
        callback(null, 'Page title for ' + urlToTest + ' is ' + title);
    });

    driver.quit();
};

0 个答案:

没有答案