Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to browser using ruby selenium webdriver

I tried to run some basic automated tests using ruby selenium webdriver. The same code works perfectly on my home computer, but fails on my work computer which is behind a proxy (which doesn't require authentication).

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

The error I get is:

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>

The browser opens with correct profile, but resulting driver variable is nil. I even tried to manually setup the proxy on the profile with no luck.

Any ideas ?

like image 517
user337620 Avatar asked Jun 22 '12 06:06

user337620


People also ask

Can we use Ruby with selenium?

Selenium and Ruby can be used together, and one of the best ways to do it is through using a gem called selenium-webdriver. With this gem, you can easily automate your test cases using other Ruby-supported frameworks – which we shall introduce below.

How does selenium Webdriver interact with browser?

Selenium works through API commands, such as GET and POST, and will function based on the Selenium script requests it gets. The requests then get sent to the HTTP server of the browser driver, as well as the browsers through HTTP.

Can selenium interact with already opened browser?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.


2 Answers

You probably have HTTP_PROXY (or http_proxy) set in your environment. The next release of selenium-webdriver (2.25) will also honor NO_PROXY/no_proxy (which you can then set to NO_PROXY=127.0.0.1). Until then you can remove the proxy from the Ruby environment before launching the browser:

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

If you need the proxy configured for Firefox to communicate with the outside world, you could try something like this:

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
like image 177
jarib Avatar answered Oct 27 '22 08:10

jarib


Usage of selenium-webdriver behind proxy has browser related specific. In short, you need to find a way of passing proxy settings to the browser instance created by webdriver.

Below is a code that works with 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
like image 41
smile-on Avatar answered Oct 27 '22 08:10

smile-on