我想通过在Python上使用Selenium Webdriver来捕获网络流量。因此,我必须使用代理(如BrowserMobProxy)
当我使用webdriver.Chrome时:
from browsermobproxy import Server
server = Server("~/browsermob-proxy")
server.start()
proxy = server.create_proxy()
from selenium import webdriver
co = webdriver.ChromeOptions()
co.add_argument('--proxy-server={host}:{port}'.format(host='localhost', port=proxy.port))
driver = webdriver.Chrome(executable_path = "~/chromedriver", chrome_options=co)
proxy.new_har
driver.get(url)
proxy.har # returns a HAR
for ent in proxy.har['log']['entries']:
print ent['request']['url']
网页已正确加载,所有请求都在HAR文件中可用和可访问。 但是当我使用webdriver.Firefox时:
# The same as above
# ...
from selenium import webdriver
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=profile, proxy = proxy.selenium_proxy())
proxy.new_har
driver.get(url)
proxy.har # returns a HAR
for ent in proxy.har['log']['entries']:
print ent['request']['url']
无法正确加载网页,并且HAR文件中的请求数小于应加载的请求数。
你知道第二段代码中的代理设置有什么问题吗?为了我的目的,我应该如何正确地使用webdriver.Firefox?
刚刚偶然发现这个项目https://github.com/derekargueta/selenium-profiler。吐出URL的所有网络数据。不应该很难破解并集成到你正在运行的任何测试中。
原始源:https://www.openhub.net/p/selenium-profiler
对我来说,下面的代码组件工作得很好。
相关问题 更多 >
编程相关推荐