Python - 使用代理(Browsermob)与远程WebDriver
我正在尝试使用Browsermob Proxy来检查一个网站的网络流量。运行以下代码后,我发现代理服务器拒绝了连接:
“代理服务器拒绝连接”
“Firefox被配置为使用一个拒绝连接的代理。”
我还没有找到一个用Python设置远程webdriver代理的例子。
server = Server("location/browsermob-proxy-2.0-beta-9/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("impression")
driver.get("https://www.google.com/")
server.stop()
driver.quit()
#success
from browsermobproxy import Server
server = Server("location/browsermob-proxy-2.0-beta-9/bin/browsermob-proxy")
server.start()
our_proxy = server.create_proxy()
from selenium import webdriver
our_browser = browser.upper()
desired_capabilities = webdriver.DesiredCapabilities.FIREFOX # Default
desired_capabilities["version"] = configs[browser]["browser-version"]
desired_capabilities["platform"] = configs[browser]["os"]
desired_capabilities["idle-timeout"] = "25"
desired_capabilities["max-duration"] = "300"
desired_capabilities["command-timeout"] = "30"
desired_capabilities["name"] = test_name
desired_capabilities["browserName"] = browser
desired_capabilities['loggingPrefs'] = {"browser":"ALL"}
this_proxy = Proxy({
"httpProxy":our_proxy.selenium_proxy().httpProxy,
"sslProxy":our_proxy.selenium_proxy().sslProxy,
"proxyType":"MANUAL",
"autodetect":False
})
this_proxy.add_to_capabilities(desired_capabilities)
driver = webdriver.Remote(
desired_capabilities = desired_capabilities
)
proxy.new_har("impression")
driver.get("https://www.google.com/")
#fails
#urllib2.URLError: <urlopen error [Errno 61] Connection refused>
server.stop()
driver.quit()
远程和Firefox配置的desired_capabilities分别是:
{'name': 'abdc', 'javascriptEnabled': True, 'idle-timeout': '25', 'command-timeout': '30', 'max-duration': '300', 'platform': 'Windows 7', 'browserName': 'firefox', 'version': '28', 'proxy': {'proxyType': 'MANUAL', 'sslProxy': 'localhost:9117', 'httpProxy': 'localhost:9117'}, 'loggingPrefs': {'browser': 'ALL'}}
{u'rotatable': False, u'takesScreenshot': True, u'acceptSslCerts': True, u'cssSelectorsEnabled': True, u'javascriptEnabled': True, u'databaseEnabled': True, u'locationContextEnabled': True, u'platform': u'Darwin', u'browserName': u'firefox', u'version': u'29.0.1', u'nativeEvents': False, u'applicationCacheEnabled': True, u'webStorageEnabled': True, u'browserConnectionEnabled': True, u'handlesAlerts': True}
我看到有个问题说这个问题已经解决了,但看起来并不是这样。
1 个回答
我之前也遇到过同样的错误。这个问题通常是因为有其他程序在使用同一个端口,导致browsermob代理无法启动。一般来说,Apache Tomcat会占用同一个服务器。
你可以选择更改Browsermob的端口。
server = Server(browsermob_location,options={'port':port_browsermob})
这里的port_browsermob就是你可以指定的端口。