使用带有 Python 的 Selenium 在 Edge 上获取 INFO 级浏览器控制台日志事件
Using Selenium with Python to get INFO level browser console log events on Edge
我正在尝试在 PyCharm 上使用 Python/Selenium 从 Edge 获取 INFO 级别的浏览器控制台日志事件,但当前配置仅来自浏览器的 returns WARNING 级别日志。我有意使用 msedge.selenium_tools 库,因为 Edge 上的 webdriver 库似乎已被弃用。
import time
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.EDGE
capabilities['loggingPrefs'] = {'browser': 'ALL'}
capabilities['acceptInsecureCerts'] = bool(True)
edge_options = EdgeOptions()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
edge_options.use_chromium = True
driver = Edge(options=edge_options, desired_capabilities=capabilities, executable_path=r"C:\Users\myuser\Documents\edgedriver_win64\msedgedriver.exe")
...
输出:
[{'level': 'WARNING', 'message': 'Message I'm getting'}]
根据你的描述,我在Selenium 4.1.0中用webdriver测试了一下,发现可以达到你的要求。并且您必须使用 ms:loggingPrefs 而不是 loggingPrefs
.
简单代码(适用于 Edge 版本 97.0.1072.62):
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
d = DesiredCapabilities.EDGE
d['ms:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Edge(capabilities=d,executable_path = r"C:\Users\Administrator\Desktop\msedgedriver.exe")
driver.get('<your website url>')
entry = driver.get_log("browser")
print(entry)
编辑:
经过一番测试,发现使用msedge.selenium_tools
库也需要对相应的代码进行类似的修改,使其在this doc的基础上工作,但没有找到相应的文档(也许我错过了)。
下面的代码示例:
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
capabilities = DesiredCapabilities.EDGE
capabilities['ms:loggingPrefs'] = {'browser': 'ALL'}
capabilities['acceptInsecureCerts'] = bool(True)
# load the desired webpage
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
edge_options.set_capability("ms:edgeOptions",capabilities)
driver = Edge(options = edge_options, executable_path=r"C:\Users\Administrator\Desktop\msedgedriver.exe")
driver.get('https://localhost:44356/Index.html')
# print messages
entry = driver.get_log("browser")
print(entry)
结果如下:
希望对您有所帮助。
我正在尝试在 PyCharm 上使用 Python/Selenium 从 Edge 获取 INFO 级别的浏览器控制台日志事件,但当前配置仅来自浏览器的 returns WARNING 级别日志。我有意使用 msedge.selenium_tools 库,因为 Edge 上的 webdriver 库似乎已被弃用。
import time
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.EDGE
capabilities['loggingPrefs'] = {'browser': 'ALL'}
capabilities['acceptInsecureCerts'] = bool(True)
edge_options = EdgeOptions()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
edge_options.use_chromium = True
driver = Edge(options=edge_options, desired_capabilities=capabilities, executable_path=r"C:\Users\myuser\Documents\edgedriver_win64\msedgedriver.exe")
...
输出:
[{'level': 'WARNING', 'message': 'Message I'm getting'}]
根据你的描述,我在Selenium 4.1.0中用webdriver测试了一下,发现可以达到你的要求。并且您必须使用 ms:loggingPrefs 而不是 loggingPrefs
.
简单代码(适用于 Edge 版本 97.0.1072.62):
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
d = DesiredCapabilities.EDGE
d['ms:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Edge(capabilities=d,executable_path = r"C:\Users\Administrator\Desktop\msedgedriver.exe")
driver.get('<your website url>')
entry = driver.get_log("browser")
print(entry)
编辑:
经过一番测试,发现使用msedge.selenium_tools
库也需要对相应的代码进行类似的修改,使其在this doc的基础上工作,但没有找到相应的文档(也许我错过了)。
下面的代码示例:
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
capabilities = DesiredCapabilities.EDGE
capabilities['ms:loggingPrefs'] = {'browser': 'ALL'}
capabilities['acceptInsecureCerts'] = bool(True)
# load the desired webpage
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
edge_options.set_capability("ms:edgeOptions",capabilities)
driver = Edge(options = edge_options, executable_path=r"C:\Users\Administrator\Desktop\msedgedriver.exe")
driver.get('https://localhost:44356/Index.html')
# print messages
entry = driver.get_log("browser")
print(entry)
结果如下:
希望对您有所帮助。