在Selenium Webdriver中为使用Ruby的PhantomJS设置自定义用户代理
我今晚已经在网络上工作了大约3-4个小时。我已经尝试了我遇到的每个建议。我甚至已经检查了“能力”的对象我硒驱动程序对象上,以确保它实际上是设置在那里,而事实上,它是:在Selenium Webdriver中为使用Ruby的PhantomJS设置自定义用户代理
#<Selenium::WebDriver::Remote::Capabilities:0x00000007475cf0
@capabilities=
{:browser_name=>"phantomjs",
:version=>"1.9.7",
:platform=>:"linux-unknown-64bit",
:javascript_enabled=>true,
:css_selectors_enabled=>true,
:takes_screenshot=>true,
:native_events=>true,
:rotatable=>false,
:firefox_profile=>nil,
:proxy=>#<Selenium::WebDriver::Proxy:0x00000007475908 @type=:direct>,
"driverName"=>"ghostdriver",
"driverVersion"=>"1.1.0",
"handlesAlerts"=>false,
"databaseEnabled"=>false,
"locationContextEnabled"=>false,
"applicationCacheEnabled"=>false,
"browserConnectionEnabled"=>false,
"webStorageEnabled"=>false,
"acceptSslCerts"=>false,
"proxy"=>{"proxyType"=>"direct"},
"phantomjs.page.settings.userAgent"=>
"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:27.0) Gecko/20100101 Firefox/27.0"}>
除了“phantomjs.page.settings.userAgent”,我已经尝试了“userAgent”等等。我在过去的3-4个小时内可以在网上找到的每一件事,我都试过了。显然,在2013年初附近,这是一个相当普遍的问题,我说的解决方案显然是常见的解决方案。这些都不是工作,而事实上,我知道这是肯定的,从该位的信息(注意User-Agent为“红宝石”):
UNCAUGHT EXCEPTION: {"errorMessage"=>"Element is not currently visible and may not be manipulated",
"request"=>
{"headers"=>
{"Accept"=>"application/json",
"Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"Connection"=>"close",
"Content-Length"=>"2",
"Content-Type"=>"application/x-www-form-urlencoded",
"Host"=>"localhost:9876",
"User-Agent"=>"Ruby"},
"httpVersion"=>"1.1",
"method"=>"POST",
"post"=>"{}",
"postRaw"=>"{}",
"url"=>"/click",
"urlParsed"=>
{"anchor"=>"",
"query"=>"",
"file"=>"click",
"directory"=>"/",
"path"=>"/click",
"relative"=>"/click",
"port"=>"",
"host"=>"",
"password"=>"",
"user"=>"",
"userInfo"=>"",
"authority"=>"",
"protocol"=>"",
"source"=>"/click",
"queryKey"=>{},
"chunks"=>["click"]},
"urlOriginal"=>
"/session/a03cc440-4f5c-11e4-8854-ed9c22bf60af/element/%3Awdc%3A1412822036214/click"}}
不幸的是,有更多的信息和讨论这些Selenium问题以及其他许多问题,if you're using Java。在这一点上,我希望通过Java为这个项目工作的每一种方式,但现在我拥有30,000行代码,这些代码在过去的2个月中完全由我自己编写。至少现在放弃这项工作不仅会对我个人造成破坏性影响,而且会对我的工作造成灾难性的影响。
什么给?我是否真的需要深入挖掘和定制源代码才能获得我想要的功能,还是现在实现了真的?我再次看到了2013年初的所有答案,但它们不适用于我,我不知道为什么,或者如何轻松解决它。我正处于最后期限,所以这开始变得非常紧张。
有没有人有我的想法?请记住,我使用的是Ruby,而不是Java。
Selenium-webdriver是2.43。 PhantomJS是1.9.7。 GhostDriver是1.1.0。
对我来说,这似乎令人难以置信,无法修改您的User-Agent。
请让我知道,如果我可以提供任何其他可能的帮助信息。
如果您愿意分享一些想法或信息,让我指出正确的方向,
因为moonfly在他对我的问题的评论中提到,这最终不是“可能”开箱即用。但是,这样做相对容易。我应该注意,这并没有解决我的特殊问题。我可能天真地认为,由于User-Agent'Ruby',我得到了奇怪的结果。事实证明,我花了好几个小时,没有想到这件事。我仍然得到相同的结果。
但是,对于那些对你有帮助的人来说,你必须为Selenium制作一个快速的猴子补丁。我知道,讨厌,粗暴,丑陋,哈克。但是,它的确有窍门。
请注意,这是一个用于Ruby的selenium-webdriver版本2.43.0的猴子补丁程序 - 如果您有其他版本,则无法保证此功能可行。
module Selenium
module WebDriver
module Remote
module Http
class Default < Common
private
def request(verb, url, headers, payload, redirects = 0)
# THIS IS WHERE OUR CUSTOM CHANGE BEGINS
headers.merge!('User-Agent' => 'Whatever User Agent You May Want')
# THIS IS WHERE OUR CUSTOM CHANGE ENDS
request = new_request_for(verb, url, headers, payload)
retries = 0
begin
response = response_for(request)
rescue Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EADDRINUSE
# a retry is sometimes needed on Windows XP where we may quickly
# run out of ephemeral ports
#
# A more robust solution is bumping the MaxUserPort setting
# as described here:
#
# http://msdn.microsoft.com/en-us/library/aa560610%28v=bts.20%29.aspx
raise if retries >= MAX_RETRIES
request = new_request_for(verb, url, headers, payload)
retries += 1
retry
rescue Errno::ECONNREFUSED => ex
if use_proxy?
raise ex.class, "using proxy: #{proxy.http}"
else
raise
end
end
if response.kind_of? Net::HTTPRedirection
raise Error::WebDriverError, "too many redirects" if redirects >= MAX_REDIRECTS
request(:get, URI.parse(response['Location']), DEFAULT_HEADERS.dup, nil, redirects + 1)
else
create_response response.code, response.body, response.content_type
end
end
end
end
end
end
end
一个更像样的猴子修补Rubys Selenium Webdriver的方式是: 'Selenium :: WebDriver :: Remote :: Http :: Common :: DEFAULT_HEADERS ['User-Agent'] ='无论您想要的用户代理 – stmllr 2016-05-11 11:19:00
在Java中,我做了以下内容:(PhantomJS 1.9.8,硒2.39)
String userAgent = "Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; LG-LU3000 Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);
PhantomJSDriver driver = new PhantomJSDriver(caps);
也许有对Ruby类似的设置选项。看看“page.settings.userAgent”
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs('phantomjs.page.settings.userAgent' => 'some user Agent')
@instance = Selenium::WebDriver.for(:remote,:url=>'http://localhost:8910',:desired_capabilities=>capabilities)
从来没有试过自己,但检查:https://github.com/mururu/capybara-user_agent。 根据定义的内容,它基本上是'Capybara.current_session.driver.add_headers('User-Agent'=> user_agent)'或'Capybara.current_session.driver.header('User-Agent',user_agent) 。 – moonfly 2014-10-09 04:33:33
呃,好像它不会工作,因为http://stackoverflow.com/a/15647143/2117020。所以,我猜,最好的方法是从Selenium切换到Poltergeist(https://github.com/teampoltergeist/poltergeist)。它似乎支持'add_headers'。 – moonfly 2014-10-09 04:38:23
那真是太棒了!我认为你需要使用水豚来使用Poltergeist,但是我会更多地查看代码,并且看看我能够掀起什么。 谢谢。让我知道你是否有更多的想法。 – 2014-10-09 19:35:55