Home > Article > Backend Development > Selenium exception handling in Python
This article mainly introduces Selenium exception handling in PHPPython. It has certain reference value. Now I share it with you. Friends in need can refer to it.
During the execution of automated tests, it is inevitable that there will be If an error/exception occurs, for example, if the test script does not find the corresponding element, a NoSuchElementException will be thrown immediately. Don't be afraid at this time, there must be something wrong with the test script or the test environment! So how to deal with it is the key? Because there is usually only a local problem, in order to allow the script to continue executing, we can use try...except...raise to catch the exception. After catching the exception, the corresponding exception cause can be printed out, which facilitates analysis of the exception cause.
The following will give an example. When an exception is thrown, the information is printed on the console and the current browser window is intercepted. This can be used as a basis for subsequent bugs to better locate the problem for the corresponding developer. The code is as follows:
import unittest from selenium import webdriver from selenium.common.exceptions import NoSuchElementException #导入NoSuchElementException class ExceptionTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get("https://www.baidu.com") def test_exception(self): driver = self.driver try: search_text = driver.find_element_by_id("ss") self.assertEqual('百度一下', search_text.get_attribute("value")) except NoSuchElementException: file_name = "no_such_element.png" #driver.save_screenshot(file_name) driver.get_screenshot_as_file(file_name) raise #抛出异常,注释后则不抛出异常 def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2)
There is an exception when running, and the result is as follows:
## WebDriver is used in the above code The built-in method of capturing the screen and saving it, such as the save_screenshot(filename) method and save_screenshot_as_file(filename) method here, when the test exception is thrown, capture the browser screen at the same time and save it in the specified path with a custom image file name (above) code is the current path).import unittest from selenium import webdriver from selenium.common.exceptions import ElementNotVisibleException #导入ElementNotVisibleException class ExceptionTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get("https://www.baidu.com") def test_exception(self): driver = self.driver try: login = driver.find_element_by_name("tj_login") login.click() except ElementNotVisibleException: raise def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2)There is an exception during operation, the result is as follows: The following will list common exceptions in selenium: Related recommendations:
A simple analysis of the xlsxwriter library in python
For loop and range built-in functions in python
In-depth understanding of the time module in python
The above is the detailed content of Selenium exception handling in Python. For more information, please follow other related articles on the PHP Chinese website!