2017-10-20 101 views
0

试图获得AWS Lambda函数以在.NET Core上运行Selenium。这里是代码:如何使用.net核心在AWS Lambda中运行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:在OpenQA.Selenium.DriverService.Start上http://localhost:41663/ 无法启动驱动程序服务() 在OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(命令commandToExecute) 在OpenQA.Selenium.Remote.RemoteWebDriver.Execute(字符串driverCommandToExecute,Dictionary`2参数) 在OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities) 在OpenQA.Selenium.Remote.Re moteWebDriver..ctor(ICommandExecutor commandExecutor,ICapabilities desiredCapabilities) 在OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService服务,InternetExplorerOptions选项,时间跨度的CommandTimeout) 在OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService服务,InternetExplorerOptions选项) 在OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerOptions选项) 在OpenQA.Selenium.IE.InternetExplorerDriver..ctor() 在InstagramMagic.Function.FunctionHandler(ILambdaContext上下文)

+0

最好不要用在λ当地司机,你最好通过保持外部硒网格,然后在你的脚本 –

回答

0

这是可能,但到目前为止,我只有运气才能使它与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(); 
 
};

我确实有这种用在GitHub上的视频教程可运行打包压缩,有更详细的解释。在zip文件中的高峰,以了解应该如何布置软件包。 https://blackboard.github.io/lambda-selenium/

另外,我代表您为可运行的.net核心示例提交了一个问题。

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

+0

意想不到使用网格网址服了!我会给它一个镜头。感谢这个例子! – scottndecker