如何配置PhantomJSDriver以尽可能地模拟我的Google Chrome浏览器?

CS *_*wis 4 c# configuration phantomjs selenium-webdriver

以下是我的开发环境的详细信息:

-Visual Studio 2012 Ultimate与更新4

-Google Chrome版本40.0.2214.94 m

-Windows 7 Professional,具有32位操作系统

我的Google Chrome浏览器的用户代理字符串是:

Mozilla/5.0(Windows NT 6.1)AppleWebKit/537.36(KHTML,与Gecko一样)Chrome/40.0.2214.94 Safari/537.36

我的Automated UI测试代码中的C#代码如下:

var options = new PhantomJSOptions();

// Chrome User Agent ( Chrome Version 40.0.2214.94 m ) 
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36");

driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\packages\PhantomJS.1.9.8\tools\phantomjs"), options);

url = new Uri("http://localhost:2816/");

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(90.00));

wait.Until(ExpectedConditions.ElementIsClickable( By.XPath("//a[text()='Users']")));

IWebElement btn = waitArg.Until<IWebElement>((d) => {
 try{ 
 return d.FindElement( By.XPath("//a[text()='Users']") );
 }
 catch {

        return null;
    }  
  });

  btn.Click();
Run Code Online (Sandbox Code Playgroud)

不幸的是,上面的代码很难找到元素.我相信,如果我可以配置我的代码中使用的PhantomJS驱动程序来更接近地模拟我的桌面上的Google Chrome版本40.0.2214.94 m浏览器,那么Automated UI测试代码应该能够提供更一致和准确的测试结果测试运行.

有人可以建议我是否可以对以下代码进行一些修改,以便更接近地模拟我的Google Chrome版本40.0.2214.94 m浏览器?

var options = new PhantomJSOptions();

// Chrome User Agent ( Chrome Version 40.0.2214.94 m ) 
 options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36");

 driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\packages\PhantomJS.1.9.8\tools\phantomjs"), options);
Run Code Online (Sandbox Code Playgroud)

在@ artjom-b的帮助下更新答案

        var options = new PhantomJSOptions();

        options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36");


        var service = PhantomJSDriverService.CreateDefaultService(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\packages\PhantomJS.1.9.8\tools\phantomjs"));

        service.SslProtocol = "any";
        driver = new PhantomJSDriver(service, options);
        url = new Uri("http://localhost:2816/");

        // 1280, height: 1024
        // @artjom-b strongly recommened that the Driver's Window Size be quite large.  Let's set the Window Size to quite large. 
        driver.Manage().Window.Size = new System.Drawing.Size(1280, 1024); // Size is a type in assembly "System.Drawing"
Run Code Online (Sandbox Code Playgroud)

Art*_* B. 6

PhantomJS是一个独立的(无头)浏览器.您无法配置它,因此它的行为类似于Chrome.在渲染引擎方面,没有什么可配置的.您可以执行一些操作,以便PhantomJS的行为类似于桌面浏览器.

第一件事是将视口大小设置为大的.PhantomJS默认运行,视口为400x300.vieport扩展到固定宽度的站点,但对于响应式站点保持较小.根据网站的编写方式,某些元素可能会被隐藏,无法点击.
请参阅:在Sempium驱动的PhantomJS C#中设置屏幕尺寸

第二件事是将用户代理字符串设置为最接近其功能的东西.PhantomJS 1.x基于QtWebKit的旧分支.据说Chrome 13与它最兼容.
为什么这有关系?有些网站在遇到新的用户代理时会使用一些"实验性"JavaScript.使用旧的/匹配的用户代理字符串,以便执行此操作的站点不会发送时髦的JavaScript或PhantomJS无法理解的其他奇怪的东西.谷歌做了这样做了.
用户代理字符串:

Mozilla/5.0(Windows NT 6.0)AppleWebKit/535.1(KHTML,像Gecko)Chrome/13.0.782.41 Safari/535.1

PhantomJS 1.x默认使用SSLv3,如果服务器不支持此功能(由于POODLE漏洞),则连接失败.使用any以便PhantomJS接受它理解的所有SSL/TLS连接.
请参阅:答:Selenium Webdriver + PhantomJS保持在大约:空白的特定网站

PhantomJS 1.x不支持Function.prototype.bind其他内容.C#的selenium语言绑定支持执行PhantomJS脚本片段(不是页面脚本),通过PhantomJSDriver.ExecutePhantomJS()它可以运行垫片以使用一些必要的功能来改进PhantomJS.评论中提供的相关提交.

有些事你不能真正做些什么:

  • 点击下载链接不会下载任何内容.PhantomJS不会触发下载(使用XHR进行手动下载的最长持续时间为30秒).
  • 无法打开PDF.

  • @ArtjomB.我不确定"提供SO链接"是什么意思,但是在撰写本文时,这个功能已经在.NET绑定中使用了将近一年.您可以在https://github.com/SeleniumHQ/selenium/commit/72760b0c3283f9933d5ef32b657ee9a3aba99aa7上看到项目源代码的GitHub镜像中添加的提交. (2认同)