我希望您能够用Python的Selenium Webdriver来捕获网络流量。因此,我必须使用代理(如BrowserMobProxy)。
当我使用webdriver.Chrome时:
网页已经正确加载,所有请求在HAR文件中都可用和可访问。 但当我使用webdriver.Firefox时:
当我使用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达到我的目的?
profile.accept_untrusted_certs = True
)。 - user3468054