2012-06-22 21 views
9

Ho provato a eseguire alcuni test automatici di base utilizzando il webdriver di selenio rubino. Lo stesso codice funziona perfettamente sul mio computer di casa, ma non funziona sul mio computer di lavoro che si trova dietro un proxy (che non richiede l'autenticazione).Impossibile connettersi al browser utilizzando il webdriver di selenio rubino

driver = Selenio :: WebDriver.for: firefox,: profilo => 'default'

L'errore che ottengo è:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.24.0/lib/selenium/webdriver/remote/http/common.rb:66:in `create_response': unexpected response, code= 
403, content-type="text/html" (Selenium::WebDriver::Error::WebDriverError) 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> 
<TITLE>ERROR: The requested URL could not be retrieved</TITLE> 
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE> 
</HEAD><BODY> 
<H1>ERROR</H1> 
<H2>The requested URL could not be retrieved</H2> 
<HR noshade size="1px"> 
<P> 
While trying to retrieve the URL: 
<A HREF="http://127.0.0.1:7055/hub/session">http://127.0.0.1:7055/hub/session</A> 
<P> 
The following error was encountered: 
<UL> 
<LI> 
<STRONG> 
Access Denied. 
</STRONG> 
<P> 
Access control configuration prevents your request from 
being allowed at this time. Please contact your service provider if 
you feel this is incorrect. 
</UL> 

Il browser si apre con profilo corretto, ma risulta variabile conducente è nulla. Ho anche provato a configurare manualmente il proxy sul profilo senza fortuna.

Qualche idea?

risposta

16

Probabilmente hai impostato HTTP_PROXY (o http_proxy) nel tuo ambiente. La prossima versione di selenium-webdriver (2.25) onorerà anche NO_PROXY/no_proxy (che è possibile impostare su NO_PROXY = 127.0.0.1). Fino ad allora è possibile rimuovere il proxy dall'ambiente di Ruby prima di avviare il browser:

ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil 
driver = Selenium::WebDriver.for :firefox 

Se è necessario il proxy configurato per Firefox per comunicare con il mondo esterno, si potrebbe provare qualcosa di simile:

proxy = Selenium::WebDriver::Proxy.new(:http => ENV['HTTP_PROXY'] || ENV['http_proxy']) 
ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil 
driver = Selenium::WebDriver.for :firefox, :proxy => proxy 
+0

sto usando il selenio-WebDriver 2.26.0, impostando NO_PROXY = 127.0.0.1 ha risolto il problema. –

+0

Wow, questo ha funzionato davvero per me. –

3

L'utilizzo del selenio-webdriver dietro al proxy ha specifiche relative al browser. In breve, è necessario trovare un modo per trasferire le impostazioni proxy all'istanza del browser creata da Webdriver.

Di seguito è riportato un codice che funziona con Firefox.

#Firefox keeps proxy settings in profile. 
profile = Selenium::WebDriver::Firefox::Profile.new 
profile.proxy = Selenium::WebDriver::Proxy.new(:http => "192.168.1.1:3128") 
driver = Selenium::WebDriver.for :firefox, :profile => profile 

driver.navigate.to "http://google.com" 
puts driver.title 
driver.quit 
-1
require 'rubygems' 
require 'selenium-webdriver' 
ENV['NO_PROXY']="127.0.0.1" 
driver = Selenium::WebDriver.for :firefox 
driver.get "http://google.com" 
+4

Benvenuti in StackOverflow! Forse puoi aggiungere del testo per spiegare la tua risposta ?! – franssu

Problemi correlati