【问题标题】:Exception message is not printed when using WebDriverWait from Selenium through Python通过 Python 从 Selenium 使用 WebDriverWait 时不打印异常消息
【发布时间】:2019-11-07 16:15:25
【问题描述】:

如果 xpath 位于 try-except() 块中,我可以看到异常消息self.driver.find_element_by_xpath()。但是当我向元素添加显式等待时,如果 xpath 不存在或错误,则错误消息为空白。

如何在 except 块中打印错误消息? 如果我运行这个,对于第一个,能够显示 e,下一个是空白的

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)

【问题讨论】:

  • 你确定WebWebDriverWait没有错字吗?
  • 请编辑您的问题并正确格式化您的代码。事实上,它真的很难阅读。
  • JaSON,如果 xpath 正确,则脚本运行良好。但是如果我只是用一些错误的输入更改 xpath 来测试错误消息,我将无法引发错误消息。

标签: python-3.x selenium selenium-webdriver exception timeoutexception


【解决方案1】:

当你使用find_element_by_* 并且没有找到元素时,NoSuchElementException 被抛出。

但是如果你将WebDriverWaitexpected_conditions 结合起来,则在失败时会抛出TimeoutException

此时,值得一提的是,您应该始终捕获所需的异常,即NoSuchElementExceptionTimeoutException,但不是基本异常,即Exception,以保持测试干净.

一个简单的测试来详细观察NoSuchElementExceptionTimeoutException

  • 代码块:

    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()
    
  • 控制台输出:

    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)
    

结论

您可以通过 message控制台输出 看到为 NoSuchElementException 正确定义异常部分,但 TimeoutException 未定义消息 部分。因此它是空白的。

【讨论】:

  • 这不能回答问题。他在他的问题when I add explicit wait to element,the error message is blank 中说,你解释了很多东西,当捕获到显式等待时仍然打印空白错误消息。
  • 是的,我也试过 NoSuchElementException 和 TimeoutException,没有输出。我想在使用显式等待时打印错误消息。
  • @pdas 你现在真的花点时间至少读过一次答案吗?
  • @DebanjanB 根据您打印 (toe.args) 时第二个 try-exception 块的控制台输出:('',无,无)。但我想打印 ('no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name=\'q123\']"}\n (Session info : chrome=75.0.3770.100)', None, None) 作为错误信息
  • 您需要从第 1 行开始阅读答案。 1. find_element_by_* 在定义消息的地方引发 NoSuchElementException,但在未定义消息的地方 WebDriverWait 引发 TimeoutException。所以你看到 blank
猜你喜欢
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 2020-10-26
  • 2020-11-26
  • 1970-01-01
相关资源
最近更新 更多