2014-10-09 90 views
2

我今晚已經在網絡上工作了大約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。

請讓我知道,如果我可以提供任何其他可能的幫助信息。

如果您願意分享一些想法或信息,讓我指出正確的方向,

+1

從來沒有試過自己,但檢查: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

+1

呃,好像它不會工作,因爲http://stackoverflow.com/a/1564714​​3/2117020。所以,我猜,最好的方法是從Selenium切換到Poltergeist(https://github.com/teampoltergeist/poltergeist)。它似乎支持'add_headers'。 – moonfly 2014-10-09 04:38:23

+0

那真是太棒了!我認爲你需要使用水豚來使用Poltergeist,但是我會更多地查看代碼,並且看看我能夠掀起什麼。 謝謝。讓我知道你是否有更多的想法。 – 2014-10-09 19:35:55

回答

0

因爲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 
+0

一個更像樣的猴子修補Rubys Selenium Webdriver的方式是: 'Selenium :: WebDriver :: Remote :: Http :: Common :: DEFAULT_HEADERS ['User-Agent'] ='無論您想要的用戶代理 – stmllr 2016-05-11 11:19:00

5

在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」

1
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) 
相關問題