作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据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)
输出:
{'level': 'INFO', 'message': '{"message":{"method":"Network.loadingFinished","params":{"encodedDataLength":0,"requestId":"A9C189F82C9479C83512A78F9FF9F3EA","shouldReportCorbBlocking":false,"timestamp":3369761.457106}},"webview":"A7D8907A1D283AC9EC1B03BC3C786243"}', 'timestamp': 1552406406094}
{'level': 'INFO', 'message': '{"message":{"method":"Page.frameNavigated","params":{"frame":{"id":"A7D8907A1D283AC9EC1B03BC3C786243","loaderId":"A9C189F82C9479C83512A78F9FF9F3EA","mimeType":"text/plain","securityOrigin":"://","url":"data:,"}}},"webview":"A7D8907A1D283AC9EC1B03BC3C786243"}', 'timestamp': 1552406406094}
{'level': 'INFO', 'message': '{"message":{"method":"Page.loadEventFired","params":{"timestamp":3369761.458842}},"webview":"A7D8907A1D283AC9EC1B03BC3C786243"}', 'timestamp': 1552406406094}
{'level': 'INFO', 'message': '{"message":{"method":"Page.frameStoppedLoading","params":{"frameId":"A7D8907A1D283AC9EC1B03BC3C786243"}},"webview":"A7D8907A1D283AC9EC1B03BC3C786243"}', 'timestamp': 1552406406094}
{'level': 'INFO', 'message': '{"message":{"method":"Page.domContentEventFired","params":{"timestamp":3369761.45929}},"webview":"A7D8907A1D283AC9EC1B03BC3C786243"}', 'timestamp': 1552406406094}
{'level': 'INFO', 'message': '{"message":{"method":"Page.frameResized","params":{}},"webview":"A7D8907A1D283AC9EC1B03BC3C786243"}', 'timestamp': 1552406406419}
我也尝试在字典中使用“true”和“false”字符串,但捕获的日志记录没有任何变化。
caps['perfLoggingPrefs'] = {
'enableNetwork' : 'true',
'enablePage' : 'false',
'enableTimeline' : 'false'
}
使用
最佳答案
你必须将 perfLoggingPrefs
作为 chromeOptions 传入,你可以通过 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
时,Page
事件仍会显示。另一方面,过滤确实在 enableNetwork
上起作用,所以可能是 chromedriver 中的错误。
您始终可以手动过滤日志,例如:
filtered = [p for p in perfs if not json.loads(p["message"])["message"]["method"].startswith('Page')]
关于python selenium perfLoggingPrefs过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55126252/
我是一名优秀的程序员,十分优秀!