python selenium perfLoggingPrefs筛选

2024-05-18 06:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

根据chromedriver documentation我应该能够在记录性能时关闭页面域中的事件。我尝试过设置perfLoggingPrefs,但仍能获得页面事件。我设置正确了吗?在

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.add_argument("--disable-extensions")
options.add_argument("--allow-running-insecure-content")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--disable-single-click-autofill")
options.add_argument("--disable-autofill-keyboard-accessory-view[8]")
options.add_argument("--disable-full-form-autofill-ios")

options.headless = True
options.add_argument('--disable-gpu')

caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {
    'browser': 'ALL',
    'performance' : 'ALL',
    }
caps['perfLoggingPrefs'] = {
    'enableNetwork' : True,
    'enablePage' : False,
    'enableTimeline' : False
    }

driver = webdriver.Chrome(options=options, desired_capabilities=caps)

###  connect to my site, do some actions then I call
perfs = driver.get_log('performance')
for row in perfs:
    print(perfs)

输出: 在

^{pr2}$

我试着在字典中使用'true'和'false'字符串,而没有对捕获的日志进行任何更改。在

caps['perfLoggingPrefs'] = {
    'enableNetwork' : 'true',
    'enablePage' : 'false',
    'enableTimeline' : 'false'
    }

使用

  • python 3.7
  • 铬驱动2.38.552518(72.0.3626.121)
  • python selenium 3.141.0版

Tags: fromimportaddfalseselenium事件页面caps
1条回答
网友
1楼 · 发布于 2024-05-18 06:33:45

你必须把perfLoggingPrefs作为色差传递,你可以通过add_experimental_option()来传递

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.headless = True

caps = DesiredCapabilities.CHROME.copy()
caps['loggingPrefs'] = {
    'browser': 'ALL',
    'performance' : 'ALL',
    }

options.add_experimental_option('perfLoggingPrefs', {
    'enableNetwork' : True,
    'enablePage' : False,
    })

driver = webdriver.Chrome(options=options, desired_capabilities=caps)
driver.get('http://google.com')
perfs = driver.get_log('performance')
for row in perfs:
    print(row)

然而,即使enablePage被设置为False,仍然会出现{}事件。另一方面,过滤对enableNetwork起作用,因此chromedriver中可能有一个bug。在

您始终可以手动筛选日志,如下所示:

^{pr2}$

相关问题 更多 >