python 无头模式_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/')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值