Exception message is not printed when using WebDriverWait from Selenium through Python

冷暖自知 提交于 2020-06-17 10:14:51

问题


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)

回答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.



来源:https://stackoverflow.com/questions/56760572/exception-message-is-not-printed-when-using-webdriverwait-from-selenium-through

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!