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/')
阅读 9.2k
3 个回答
✓ 已被采纳新手上路,请多包涵

好吧,我自己解决了,写法不对,把参数写与dc就行了

新手上路,请多包涵

大佬,请问具体怎么解决的,我也遇到了同意的问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进