3
votes

I am using the Watir-Webdriver library in Ruby to check some pages. I know I can connect through a proxy using

profile = Selenium::WebDriver::Firefox::Profile.new#create a new profile
profile.proxy = Selenium::WebDriver::Proxy.new(#create proxy data for in the profile
  :http => proxyadress,
  :ftp => nil,
  :ssl => nil,
  :no_proxy => nil
)
browser = Watir::Browser.new :firefox, :profile => profile#create a browser window with this profile
browser.goto "http://www.example.com"
browser.close

However, when wanting to connect to the same page multiple times using different proxies, I have to create a new browser for every proxy. Loading(and unloading) the browser takes quite some time.

So, my question: Is there any way to change, using webdriver in ruby, the proxy adress Firefox uses to connect through while keeping the browser open?

2
Why would you need different proxies?Mark Thomas
because I am trying to test the proxies for availability.Qqwy
Maybe there is some more direct way to do that? some means to query the proxy directly to see if it's there, instead of indirectly via the 'set-it, try-a-page-and-se-if-it-loads' method you seem to be trying.Chuck van der Linden
Sorry if I wasn't clear enough. I am trying to test if certain pages are/are not blocked trough the proxies.Qqwy
It may be intentional that you cannot do this 'on the fly' or via javascript because that would open a real nasty capability to malicious sites who would just love to change your proxy setting without you knowing it so all your web traffic would flow through their site.Chuck van der Linden

2 Answers

2
votes

If you want to test whether a page is blocked when accessed through a proxy server, you can do that through a headless library. I recently had success using mechanize. You can probably use net/http as well.

I am still not sure why you need to change the proxy server for a current session.

require 'Mechanize'
session = Mechanize.new
session.set_proxy(host, port, user, pass)   
session.user_agent='Mac Safari'
session.agent.robots = true #observe of robots.txt rules
response = session.get(url)
puts response.code

You need to supply the proxy host/port/user/pass (user/pass are optional), and the url. If you get an exception, then the response.code is probably not friendly.

1
votes

You may need to use an OS level automation tool to automate going through the FF menus to change the setting as a user would.

For windows users there is the option of either the new RAutomation tool, or AutoIT. both can be used to automate things at the OS UI level, which would let you go into the browser settings and change the proxy there.

Still I'd think if you are checking a larger number of sites that the overhead to change the proxy settings would not be that much compared to all of the site navigation and waiting for pages to load etc.

Unless you are currently taking a 'row traverse' approach and changing proxy settings multiple times for each site you are checking? If that's the case I would go towards more of a by-column method (if we were to presume each column is a proxy, and each row is a site) and fire up the browser for one proxy, check all the sites, then change the proxy and re-check all the sites. That way you'd only be changing the proxy settings once for each proxy which should not add that much overhead to your script.

It might mean a little more work with storing and then reporting results at the end (if you had been writing them out a line at a time) but that's what hashes or arrays are for.