如何在selenium-webdriver中为phantomjs驱动程序设置useragent?

时间:2013-01-26 10:15:42

标签: ruby selenium-webdriver phantomjs

甚至可能吗?我目前有:

 require 'selenium-webdriver'

 phantomjs_useragent = {"phantomjs.page.settings.userAgent" => "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3"}
 driver = Selenium::WebDriver.for :phantomjs
 driver.capabilities.merge!(phantomjs_useragent)
 driver.navigate.to "http://whatsmyuseragent.com/"
 puts driver.first(:tag_name => 'body').text

得到了我,这不是我投入的用户代理:

您的用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X)AppleWebKit / 534.34(KHTML,与Gecko一样)PhantomJS / 1.8.1 Safari / 534.34

1 个答案:

答案 0 :(得分:4)

根据this suggestion

capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.userAgent" => "Custom Agent/1.0")
Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities

合并为selenium-webdriver-2.31及更高版本