我正在尝试使用 Browsermob 代理检查网站上的网络流量。通过运行以下命令:我发现代理服务器拒绝连接:
“代理服务器拒绝连接” “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()
远程和firefoxprofile的desired_capabilites分别为:
{'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':{'浏览器':'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' :真,u'browserConnectionEnabled':真,u'handlesAlerts':真}
我看过这张票,上面说问题已解决;但似乎并非如此。