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)
4
  • Are you sure that there is no typo in WebWebDriverWait? Commented Jun 25, 2019 at 19:18
  • Please edit your question and properly format your code. As it is, it's really hard to read. Commented Jun 25, 2019 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. Commented Jun 26, 2019 at 6:58
  • Possible duplicate of stackoverflow.com/questions/43922933/… Commented Jul 4, 2020 at 10:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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.
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 Did you really take some time to read the answer atleast once by now?
@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
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.