Using Selenium with Python to get INFO level browser console log events on Edge

2.3k Views Asked by At

I'm trying to get INFO level browser console log events from Edge using Python/Selenium on PyCharm but current configuration only returns WARNING level logs from browser. I'm using msedge.selenium_tools library intentionally because webdriver library on Edge seems to be deprecated.

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")

     ...

Output:

[{'level': 'WARNING', 'message': 'Message I'm getting'}]
1

There are 1 best solutions below

3
Xudong Peng On BEST ANSWER

According to your description, I tested it with webdriver in Selenium 4.1.0 and found that your requirement can be achieved. And you must to use ms:loggingPrefs instead of loggingPrefs.

Simple code(work in Edge version 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)

Edit:

After some testing, I found that using the msedge.selenium_tools library also requires a similar modification to the corresponding code to make it work based on this doc, but I didn't find the corresponding documentation(maybe I missed).

Code sample below:

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)

Result like this:

enter image description here

Hope this can help you.