首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为ruby中的selenium铬驱动程序设置代理

为ruby中的selenium铬驱动程序设置代理
EN

Stack Overflow用户
提问于 2015-03-20 17:49:27
回答 1查看 5.7K关注 0票数 5

Browsermob代理:-

代码语言:javascript
复制
https://github.com/jarib/browsermob-proxy-rb

我可以为firefox配置文件创建和设置代理,但不能在chrome上创建和设置代理。

我不知道我必须使用哪些选项才能让chrome设置代理。

我使用了以下代码:

对于firefox:-

代码语言:javascript
复制
require 'selenium/webdriver'
require 'browsermob/proxy'

server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start

proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>

profile = Selenium::WebDriver::Firefox::Profile.new #=> #<Selenium::WebDriver::Firefox::Profile:0x000001022bf748 ...>
profile.proxy = proxy.selenium_proxy

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

proxy.new_har "google"
driver.get "http://google.com"

har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"

proxy.close
driver.quit

对于铬:-

代码语言:javascript
复制
require 'selenium/webdriver'
require 'browsermob/proxy'

server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start

proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>

profile = Selenium::WebDriver::Chrome::Profile.new #=>
profile.proxy = proxy.selenium_proxy

driver = Selenium::WebDriver.for :chrome, :prefs => profile

proxy.new_har "google"
driver.get "http://google.com"

har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"

proxy.close
driver.quit

在chrome中,以下行抛出错误

代码语言:javascript
复制
profile.proxy = proxy.selenium_proxy

错误:- NoMethodError:#的未定义方法`proxy=

如何在chrome驱动程序配置文件上设置代理?

EN

回答 1

Stack Overflow用户

发布于 2015-03-25 06:29:51

嘿,不确定是否已经解决了这个问题,我遇到了同样的问题,但最终通过使用以下代码完成了工作:

代码语言:javascript
复制
require 'selenium/webdriver'
require 'browsermob/proxy'

server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start

proxy = Selenium::WebDriver::Proxy.new(:http => @proxy.selenium_proxy.http)
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy)
driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)

然后,您可以使用har= proxy.har保存har文件

通过使用这一点,基本上可以避免chromedriver指向错误的端口和本地主机。此外,此方法也适用于IEdriver,只需将上限更改为

代码语言:javascript
复制
Selenium::WebDriver::Remote::Capabilities.internetexplorer(:proxy => proxy)

希望这种方法能有所帮助:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29163640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档