Getting console.log output from Chrome with Selenium Python API bindings.

SeleniumAutomation TestingTesting Tools

We can get console.log output from Chrome with Selenium Python API bindings. We will perform this with the DesiredCapabilities class. We shall enable the logging from the browser with DesiredCapabilities.Chrome setting.

We have to pass this browser capability to the driver object by passing it as a parameter to the Chrome class. To enable logging we shall set the property goog:loggingPrefs of the browser to 'browser':'ALL'.

Syntax

Syntax:dc = DesiredCapabilities.CHROME
dc['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=dc)

Example

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import
DesiredCapabilities
#set browser log
dc = DesiredCapabilities.CHROME
dc['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe",
desired_capabilities=dc)
#launch browser
driver.get ("https://www.tutorialspoint.com/index.htm")
#obtain with get_log()
for e in driver.get_log('browser'):
   print(e)
driver.quit()

Output

raja
Published on 30-Jan-2021 12:55:30
Advertisements