无法使用ruby selenium webdriver连接到浏览器

我尝试使用ruby selenium webdriver运行一些基本的自动化测试。 相同的代码在我的家用计算机上完美运行,但在代理后面的工作计算机上失败(不需要身份validation)。

driver = Selenium :: WebDriver.for:firefox,:profile =>’default’

我得到的错误是:

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)   ERROR: The requested URL could not be retrieved   

ERROR

The requested URL could not be retrieved


While trying to retrieve the URL: http://127.0.0.1:7055/hub/session

The following error was encountered:

  • Access Denied.

    Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.

浏览器打开时显示正确的配置文件,但生成的驱动程序变量为nil。 我甚至试图在配置文件上手动设置代理而没有运气。

有任何想法吗 ?

您可能在您的环境中设置了HTTP_PROXY(或http_proxy)。 selenium-webdriver(2.25)的下一个版本也将遵循NO_PROXY / no_proxy(然后可以设置为NO_PROXY = 127.0.0.1)。 在此之前,您可以在启动浏览器之前从Ruby环境中删除代理:

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

如果您需要为Firefox配置的代理与外界通信,您可以尝试这样的事情:

 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 

在代理后面使用selenium-webdriver具有与浏览器相关的特定function。 简而言之,您需要找到一种将代理设置传递给webdriver创建的浏览器实例的方法。

以下是适用于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 
 require 'rubygems' require 'selenium-webdriver' ENV['NO_PROXY']="127.0.0.1" driver = Selenium::WebDriver.for :firefox driver.get "http://google.com"