How to get browser console error messages using Selenium WebDriver + python

10,455

this Error: looks like a JS error being printed:

[{u'source': u'deprecation', u'message': 
 u"https://xxxxx/static/vendor/vendor.bundle.js?v=16 902 Synchronous 
 XMLHttpRequest on the main thread is deprecated because of its detrimental 
 effects to the end user's experience. For more help, check 
 https://xhr.spec.whatwg.org/.", u'timestamp': 1515593047810, u'level': 
 u'WARNING'}]

I believe what that is outputing is what you are looking for.

Example from console on https://stackoverflow.com/: enter image description here

and this is my output from for log in driver.get_log('browser'): print(log):

{'level': 'SEVERE', 'message': 'https://secure.quantserve.com/quant.js - 
Failed to load resource: net::ERR_CONNECTION_RESET', 'source': 'network', 
'timestamp': 1515630280361}
{'level': 'SEVERE', 'message': 'https://js-sec.indexww.com/ht/p/185901-
159836282584097.js - Failed to load resource: net::ERR_TIMED_OUT', 'source': 
'network', 'timestamp': 1515630288646}
{'level': 'SEVERE', 'message': 
'https://www.googletagservices.com/tag/js/gpt.js - Failed to load resource: 
net::ERR_TIMED_OUT', 'source': 'network', 'timestamp': 1515630288785}
Share:
10,455

Related videos on Youtube

Anusha Konduru
Author by

Anusha Konduru

Updated on June 04, 2022

Comments

  • Anusha Konduru
    Anusha Konduru almost 2 years

    Console error png We have been trying to print the console errors for the particular site by using the following code. But we are unable to catch console errors. Can anyone give quick response But we are getting

    Line of Code :

    driver.get_log('browser')
    

    Error :

    [{u'source': u'deprecation', u'message': u"https://xxxxx/static/vendor/vendor.bundle.js?v=16 902 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", u'timestamp': 1515593047810, u'level': u'WARNING'}]
    

    Line of Code :

    driver.get_log('driver')
    

    Error :

    [{u'timestamp': 1515593061561, u'message': u'Unable to evaluate script: disconnected: not connected to DevTools\n', u'level': u'WARNING'}, {u'timestamp': 1515593071847, u'message': u'Unable to evaluate script: disconnected: not connected to DevTools\n', u'level': u'WARNING'}]
    

    Code Block :

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    capabilities = DesiredCapabilities.CHROME
    capabilities['loggingPrefs'] = { 'browser':'ALL' }
    driver = webdriver.Chrome(desired_capabilities=capabilities)
    driver.get('url')
    elem = driver.find_element_by_id('username')
    elem.send_keys('xxxx')
    elem1 = driver.find_element_by_name('password')
    elem1.send_keys('xxxx')
    elem2 = driver.find_element_by_class_name('btn-info')
    elem2.click()
    driver.get('url')
    for entry in driver.get_log('browser'):
        print entry
    
    • Ron Norris
      Ron Norris over 6 years
      Not sure what you want to see exactly, but you could import the exceptions module for selenium: from selenium.common.exceptions import *. Then you can write try/except blocks to catch and report specific Selenium exceptions.
    • Qwerty
      Qwerty over 6 years
      What do you mean by console errors?
    • Anusha Konduru
      Anusha Konduru over 6 years
      @RonNorris we have to print the errors whatever we are getting in the console.
    • Anusha Konduru
      Anusha Konduru over 6 years
      @Qwerty javascript console errors or messages
    • undetected Selenium
      undetected Selenium over 6 years
      Is it that you are trying to read the Browser Console Logs through your program?
    • mrfreester
      mrfreester over 6 years
      This seems weird, but could you try putting your for loop in a worker thread? I'd also try putting a sleep before your for loop to see if that resolves anything. If it does then you may have to wait for something on the page to make sure it's finished doing whatever it's doing.
    • Anusha Konduru
      Anusha Konduru over 6 years
      yeah @DebanjanB I am trying to read browser console errors.
    • undetected Selenium
      undetected Selenium over 6 years
      Can you help me with a valid usecase to read the browser console errors and as per your code trial print entry back to console ?
    • Majdi Barrat
      Majdi Barrat over 2 years