watir-webdriver在保持浏览器打开的情况下更改代理 [英] watir-webdriver change proxy while keeping browser open

查看:234
本文介绍了watir-webdriver在保持浏览器打开的情况下更改代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby中的Watir-Webdriver库检查一些页面。我知道我可以通过代理连接使用

  profile = Selenium :: WebDriver :: Firefox :: Profile.new#create a新配置文件
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

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?

解决方案

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.

这篇关于watir-webdriver在保持浏览器打开的情况下更改代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆