1

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, if the xpath is not present or wrong.

How do I print the error message in except block? If I run this , for 1st one, able to prent e, next one is blank

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Chrome()
driver.get("https://www.google.com/")
driver.implicitly_wait(10)

try:
    input_element=driver.find_element_by_xpath("//input[@name='q123']")
    input_element.send_keys('Name')
except Exception as e:
    print(e)

try:
    ip_ele=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//input[@name='q123']")))
    ip_ele.send_keys('Name')
except Exception as e:
    print(e)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
pdas
  • 21
  • 1
  • 3
  • Are you sure that there is no typo in `WebWebDriverWait`? – JaSON Jun 25 '19 at 19:18
  • Please edit your question and properly format your code. As it is, it's really hard to read. – JeffC Jun 25 '19 at 21:51
  • JaSON, the script is working perfectly if the xpath is correct. But I am not able to raise the error message if I just change the xpath with some wrong input, to test error message. – pdas Jun 26 '19 at 06:58
  • Possible duplicate of https://stackoverflow.com/questions/43922933/in-python-selenium-how-do-i-set-the-error-messages-for-a-wait/47030390#47030390 – Hoppo Jul 04 '20 at 10:08

1 Answers1

3

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.

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    This doesn't answer the question. He stated in his question `when I add explicit wait to element,the error message is blank` and you explained a lot of stuff and still printed an blank error message when an explicit wait is caught. – JeffC Jun 26 '19 at 13:43
  • Yea, I also tried with NoSuchElementException and TimeoutException, and there was no output. I want to print the error message when I am using the Explicit wait. – pdas Jun 27 '19 at 05:20
  • @pdas Did you really take some time to read the answer atleast once by now? – undetected Selenium Jun 27 '19 at 06:50
  • @DebanjanB As per your console output for 2nd try-exception block when you print (toe.args) it prints : ('', None, None). But I want to print ('no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name=\'q123\']"}\n (Session info: chrome=75.0.3770.100)', None, None) as error message – pdas Jun 27 '19 at 09:42
  • You need to read the answer right from line no. 1. `find_element_by_*` raises `NoSuchElementException` where the message is defined but `WebDriverWait` raises `TimeoutException` where the message is not defined. So you see _blank_ – undetected Selenium Jun 27 '19 at 10:00
  • I understood what you said. My question was that only. How can I print error message while using WebDriverWait. I know, if I use find_element_by_xpath then it will give me the error message. – pdas Jun 27 '19 at 10:30
  • @pdas It's not defined still now. Selenium clients need to implement the message first then you can retrieve – undetected Selenium Jun 27 '19 at 10:36
  • Thank u @DebanjanB. As of now in my code I am using self.driver.find to print the error message and WebDriverWait as explicit wait. Any other option you suggest? – pdas Jun 28 '19 at 09:56
  • I don't understand what exactly you mean by _...I am using self.driver.find to print..._, I don't think you can do anything more unless the message for _TimeoutException_ gets defined within the _Selenium-Python_ clients. – undetected Selenium Jun 28 '19 at 10:00