watir-webdriver 在保持浏览器打开的情况下更改代理

2024-01-23

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

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

但是,当想要使用不同的代理多次连接到同一页面时,我必须为每个代理创建一个新的浏览器。加载(和卸载)浏览器需要相当长的时间。

所以,我的问题是:有没有什么方法可以使用 ruby​​ 中的 webdriver 更改 Firefox 用于连接的代理地址,同时保持浏览器打开?


如果您想测试通过代理服务器访问页面时是否被阻止,您可以通过无头库来实现。我最近使用机械化取得了成功。您或许也可以使用 net/http。

我仍然不确定为什么您需要更改当前会话的代理服务器。

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

您需要提供代理主机/端口/用户/密码(用户/密码是可选的),并且url。如果出现异常,那么response.code可能不友好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

watir-webdriver 在保持浏览器打开的情况下更改代理 的相关文章

随机推荐