selenium 无头模式获取日志performance出错
网上看到的别人的代码,运行发现出错。
无头模式下会报invalid argument: log type 'performance' not found错误,但去掉--headless即有头模式下不出错。
请问如何解决?我需要无头下,可能查看performance日志
python代码如下:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import json
def get_redurection_chain(url):
"""
Given a url, return the urls in redirection chain and the length of the redirection chain.
The redirection chain will be checked using selenium driven chrome browser and retrieved from
browser log.
:param url: the url that will be checked.
:return: (
length of redirection chain,
a list of the urls in the redirection ordered based on the sequence they are visited,
)
"""
# landing_urls record origins->url->other intermedia urls->final_url
landing_urls = list()
landing_urls.append(url)
curr_url = url
capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = {
'performance': 'ALL',
}
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('headless')
driver = webdriver.Chrome(
desired_capabilities=capabilities,
chrome_options=options,
)
driver.get(url)
for log in driver.get_log('performance'):
log_entry = json.loads(log['message'])
if 'redirectResponse' not in log_entry['message']['params']:
continue
if log_entry['message']['params']['redirectResponse']['url'] == curr_url:
redirect_url = log_entry['message']['params']['request']['url']
landing_urls.append(redirect_url)
curr_url = redirect_url
driver.close()
return len(landing_urls), landing_urls
if __name__ == '__main__':
get_redurection_chain('http://www.baidu.com/')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我自己解决了,写法不对,把参数写与dc就行了
大佬,请问具体怎么解决的,我也遇到了同意的问题
楼主怎么解决的啊 我遇到一模一样的问题解决不了啊