2017-10-20 107 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