Селен и лямбда-хромедрайвер - PullRequest
0 голосов
/ 18 марта 2020

Я пытался преодолеть эту проблему, однако ничего не работает. Я запускаю Nodejs10 в aws лямбде, и ниже приведен мой код функции.

Я получаю следующую ошибку: я попытался повторно загрузить хроматический драйвер linux, но ничего не изменилось.

"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();
};
...