PhantonJS / Selenium Custom UserAgent Issue

时间:2017-09-08 13:31:58

标签: selenium selenium-webdriver phantomjs

阅读PhantomJS和其他无头浏览器,我看到可以设置用户代理,我的基本代码:

  var driverService = PhantomJSDriverService.CreateDefaultService();
  driverService.HideCommandPromptWindow = false;
  driverService.LoadImages = true;
  driverService.ProxyType = "none";
  var options = new PhantomJSOptions();             
  options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10");
  IWebDriver driver = new PhantomJSDriver(options);

  using (var phantomDriver = new PhantomJSDriver(driverService))
  {
   // use code here...
  }

我的问题是,在这里:

new PhantomJSDriver(driverService))

我正在设置要使用的CreateDefaultService,我不知道如何添加:

IWebDriver driver = new PhantomJSDriver(options);

要用吗?或者按原样执行,这意味着我不需要在代码中的任何位置提供:驱动程序变量,任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您需要一起使用选项和服务

var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = false;
driverService.LoadImages = true;
driverService.ProxyType = "none";

var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10");

using (var phantomDriver = new PhantomJSDriver(driverService, options))
{
    // use code here...
}