通过Python从Selenium使用WebDriverWait时未打印异常消息 [英] Exception message is not printed when using WebDriverWait from Selenium through Python
问题描述
我能够看到xpath是否在try-except()
块中作为self.driver.find_element_by_xpath()
的异常消息.但是当我向元素添加显式等待时,如果xpath不存在或错误,则错误消息为空白.
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.
如何在"except"块中打印错误消息? 如果我运行this,对于第一个能够显示e的东西,则下一个是空白
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)
推荐答案
当您使用find_element_by_*
并万一找不到元素时,
When you use find_element_by_*
and incase no element is found NoSuchElementException is thrown.
但是,如果您诱导 WebDriverWait 与抛出TimeoutException .
But if you induce WebDriverWait in conjunction with expected_conditions on failure TimeoutException is thrown.
在这一点上,值得一提的是,您应始终捕获所需的异常(即NoSuchElementException
或TimeoutException
),但不基本异常(即Exception
)以保持测试的整洁
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.
一个用于详细观察NoSuchElementException
和TimeoutException
的简单测试:
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)
您可以通过消息从控制台输出中看到,该异常是为 NoSuchElementException
正确定义的,但未为 TimeoutException
定义消息部分.因此,它变成空白.
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.
这篇关于通过Python从Selenium使用WebDriverWait时未打印异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!