2

甚至可能吗?我目前有:

 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, like Gecko) PhantomJS/1.8.1 Safari/534.34

4

1 回答 1

4

根据这个建议

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 及更高版本合并

于 2013-05-13T10:38:13.103 回答