11

我有一个Azure Web应用程序,当我在控制器上调用Action时,我想用它来屏幕刮网站。在Azure Web App上运行Selenium

var driver = new PhantomJSDriver(); 
driver.Url = "http://url.com"; 
driver.Navigate(); 
var source = driver.PageSource; 
var pathElement = driver.FindElementByXPath("//table[@class='someclassname']"); 

string innerHtml = ""; 
IJavaScriptExecutor js = driver as IJavaScriptExecutor; 
if (js != null) 
{ 
    innerHtml = (string)js.ExecuteScript("return arguments[0].innerHTML;", pathElement); 
} 
return innerHtml; 

能正常工作本地,但是当我上传到我的Azure的Web应用程序,我得到这个错误

无法启动上http://localhost:51169/

司机服务,我想这有做防火墙,因为我第一次运行应用程序时需要在我的防火墙设置中批准PhantomJS。我的问题是如何将这个工作部署到Azure中?它甚至是可能的,还是我需要将其配置为某个单元测试并在Visual Studio中运行它?

+2

你有没有找到解决你的问题?因为我在同一条船上。 – Martin

+0

你是否能够得到这个工作?你呢@Martin – paqogomez

回答

6

PhantomJS今天在Azure Web Apps运行的沙箱中不起作用。请参阅wiki以获取当前已知无法正常工作的列表以及沙箱的其他许多信息。

+0

我看,你知道像ChomeDriver在Azure工作吗? –

+1

对不起,我不知道,但它可能是值得一试。不幸的是,GDI的限制确实阻碍了这个领域的很多场景。 –

+0

非常感谢您的信息,我会尝试一下。 –

0

我会在这里发布这个在Azure上工作的片段。然而从使用至今生产,我不断收到随机连接错误,如:

无法连接到远程服务器内部消息:无法连接到远程服务器内部消息:试图访问取得以其访问权限禁止的方式插入一个套接字

完全相同的代码在控制台或Windows应用程序环境中工作良好。

PhantomJSDriver driver = null; 
     PhantomJSDriverService service; 

     ServicePointManager.ServerCertificateValidationCallback = new 
      RemoteCertificateValidationCallback 
      (
       delegate { return true; } 
      ); 

     int retry = 0; 

     while (driver == null && retry < 3) 
     { 
      try 
      { 
       service = PhantomJSDriverService.CreateDefaultService(); 
       var uri = service.ServiceUrl; 
       var port = service.Port; 
       service.LocalToRemoteUrlAccess = true; 
       var ghostDriverPath = service.GhostDriverPath; 
       service.HideCommandPromptWindow = true; 
       service.Start(); 

       var options = new PhantomJSOptions(); 
       driver = new PhantomJSDriver(service, options); 
      } 
      catch (Exception ex) 
      { 
       if (driver != null) 
       { 
        driver.Close(); 
        driver.Quit(); 
        driver = null; 
       } 
       Thread.Sleep(retry * 1500); 

       ServiceAudit.Default.TraceDebug($"Starting web driver failed on {retry} try"); 
      } 
      retry++; 
     } 

     if (driver == null) 
     { 
      ServiceAudit.Default.TraceError($"Web driver could not be started"); 
     } 

     return driver;