python 使用selenium获取控制台显示数据(日志数据)
·
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
caps = {
"browserName": "chrome",
'goog:loggingPrefs': {'browser': 'ALL'} # 开启日志性能监听
}
# 将caps添加到options中
for key, value in caps.items():
chrome_options.set_capability(key, value)
executable_path = 'path/to/your/chromedriver'
# chrome_options.binary_location = 'D:/Program Files/Google/Chrome/Application/chrome.exe'
service = Service(executable_path=executable_path)
# 使用选项和能力初始化WebDriver
driver = webdriver.Chrome(service=service, options=chrome_options)
# 导航到页面
driver.get('http://example.com')
# 获取浏览器控制台日志
logs = driver.get_log('browser')
for log in logs:
message = log['message']
if 'blackbox' in message:
# 打印获取的数据,有需要再进一步筛选
print(message)
if 'dx_token' in message:
# 打印获取的数据,有需要再进一步筛选
print(message)
# 关闭浏览器
driver.quit()
更多推荐



所有评论(0)