【问题标题】:How to get Selenium running in AWS Lambda using .net core如何使用 .net core 在 AWS Lambda 中运行 Selenium
【发布时间】:2018-04-02 03:41:33
【问题描述】:

尝试获取 AWS Lambda 函数以在 .NET Core 上运行 Selenium。这是代码:

public string FunctionHandler(ILambdaContext context)
        {
            context.Logger.LogLine("Entering function");
            try
            {
                var driver = new InternetExplorerDriver();
                context.Logger.LogLine("Navigating to URL");

                driver.Navigate().GoToUrl("http://www.google.com/");

                context.Logger.LogLine("Returning Done");
                return "Done";
            }
            catch (Exception e)
            {
                context.Logger.LogLine("Oops: " + e);
                return "Failed";
            }
        }

我在 AWS 控制台中得到的错误是:

OpenQA.Selenium.WebDriverException:无法在http://localhost:41663/ 上启动驱动程序服务 在 OpenQA.Selenium.DriverService.Start() 在 OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(命令 commandToExecute) 在 OpenQA.Selenium.Remote.RemoteWebDriver.Execute(字符串 driverCommandToExecute,Dictionary`2 参数) 在 OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities) 在 OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor,ICapabilities desiredCapabilities) 在 OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService 服务,InternetExplorerOptions 选项,TimeSpan 命令超时) 在 OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService 服务,InternetExplorerOptions 选项) 在 OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerOptions 选项) 在 OpenQA.Selenium.IE.InternetExplorerDriver..ctor() 在 InstagramMagic.Function.FunctionHandler(ILambdaContext 上下文)

【问题讨论】:

  • 最好不要在 Lambda 上使用本地驱动程序,最好保留一个外部 selenium 网格,然后在脚本中使用网格 url

标签: amazon-web-services selenium .net-core aws-lambda


【解决方案1】:

这是可能的,但到目前为止,我只是幸运地让它与 Chrome 一起工作。 AWS Lambda 正在运行 Amazon Linux 的基本版本。如果你想在它上面运行一些超出基础的东西,你必须打包一个 zip 文件并将它与所有需要的二进制文件一起部署。不幸的是,我怀疑 IE 会在 AWS Lambda 上运行。但是,它有望在 Azure 的等效服务上运行,该服务使用他们所谓的“Windows 容器”。

您必须指定 Chrome 二进制文件在包含您的函数的 Lambda 运行时文件系统中的位置,即 /var/task/。这是您尝试执行的操作的 node.js 示例,但使用的是 chromedriver。

'use strict';

exports.handler = (event, context, callback) => {
    var webdriver = require('selenium-webdriver');
    var chrome = require('selenium-webdriver/chrome');
    var builder = new webdriver.Builder().forBrowser('chrome');
    var chromeOptions = new chrome.Options();
    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(event.url);
    driver.getTitle().then(function(title) {

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

    driver.quit();
};

我实际上有一个可运行的打包 zip,在 github 上有一个视频教程,有更详细的解释。在 zip 文件中达到峰值,以了解应如何布置包。 https://blackboard.github.io/lambda-selenium/

此外,我已代表您提交了一个问题,以获取可运行的 .net 核心示例。

https://github.com/blackboard/lambda-selenium/issues/22

【讨论】:

  • 太棒了!我会试一试。谢谢你的例子!
  • .net 核心的任何运气,我都在努力解决这些设置
猜你喜欢
  • 2017-08-09
  • 2021-08-11
  • 1970-01-01
  • 2019-01-24
  • 2018-05-19
  • 2020-06-04
  • 2018-01-07
  • 2021-12-17
  • 2019-09-28
相关资源
最近更新 更多