watir-webdriver在保持浏览器打开的情况下更改代理 [英] watir-webdriver change proxy while keeping browser open
问题描述
profile = Selenium :: WebDriver :: Firefox :: Profile.new#create a新配置文件
$ p $但是,当想要使用不同的代理连接多个同一个页面时,我必须为每个代理创建一个新的浏览器。加载(和卸载)浏览器需要相当长的一段时间。
profile.proxy = Selenium :: WebDriver :: Proxy.new(#为配置文件创建代理数据
:http => proxyadress,
:ftp => nil,
:ssl => nil,
:no_proxy => nil
)
browser = Watir :: Browser.new:firefox,:profile => profile#用这个配置文件创建一个浏览器窗口
browser.gotohttp://www.example.com
browser.close
所以,我的问题:有没有什么办法可以改变,在ruby中使用webdriver,代理地址Firefox使用连接通过,同时保持浏览器打开?
解决方案如果你想测试一个页面是否被阻止当通过代理服务器进行访问时,可以通过无头库实现。我最近使用机械化取得了成功。您也可以使用net / http。
我还不确定为什么您需要更改当前会话的代理服务器。
$ p $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
您需要提供代理 host / port / user / pass < (用户/密码是可选的)和 url 。如果你得到一个异常,那么response.code可能不友好。 I am using the Watir-Webdriver library in Ruby to check some pages. I know I can connect through a proxy using 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? 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. 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. 这篇关于watir-webdriver在保持浏览器打开的情况下更改代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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
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