Skip to content Skip to sidebar Skip to footer

Exception Message Is Not Printed When Using Webdriverwait From Selenium Through Python

I am able to see exception message for if xpath is in try-except() block as self.driver.find_element_by_xpath().But when I add explicit wait to element,the error message is blank,

Solution 1:

When you use find_element_by_* and incase no element is found NoSuchElementException is thrown.

But if you induce WebDriverWait in conjunction with expected_conditions on failure TimeoutException is thrown.

At this point, it is worth to mention that you should always catch the desired exception i.e. either NoSuchElementException or TimeoutException but not the base exception i.e. Exception to keep your tests clean.

A simple test to observe the NoSuchElementException and TimeoutException in details:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException, NoSuchElementException
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    chrome_options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.google.com/")
    try:
        driver.find_element_by_xpath("//input[@name='q123']").send_keys("user10391084")
    except NoSuchElementException as nse:
        print(nse)
        print("-----")
        print(str(nse))
        print("-----")
        print(nse.args)
        print("=====")
    
    try:
        WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//input[@name='q123']"))).send_keys("user10391084")
    except TimeoutException as toe:
        print(toe)
        print("-----")
        print(str(toe))
        print("-----")
        print(toe.args)
    driver.quit()
    
  • Console Output:

    Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='q123']"}
      (Session info: chrome=75.0.3770.100)
    
    -----
    Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='q123']"}
      (Session info: chrome=75.0.3770.100)
    
    -----
    ('no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name=\'q123\']"}\n  (Session info: chrome=75.0.3770.100)', None, None)
    =====
    Message: 
    
    -----
    Message: 
    
    -----
    ('', None, None)
    

Conclusion

You can see from the Console Output through the message part of the exception is properly defined for NoSuchElementException but the message part is not defined for TimeoutException. Hence it comes blank.

Post a Comment for "Exception Message Is Not Printed When Using Webdriverwait From Selenium Through Python"