Python 中的Selenium异常处理实例代码
作者:菜鸟可米 时间:2021-03-11 12:42:11
自动化测试执行过程中,难免会有错误/异常出现,比如测试脚本没有发现对应元素,则会立刻抛出NoSuchElementException异常。这时不要怕,肯定是测试脚本或者测试环境哪里出错了!那如何处理才是关键?因为一般只是局部有问题,为了让脚本继续执行,so我们可以用try...except...raise捕获异常。该捕获异常后可以打印出相应的异常原因,这样以便于分析异常原因。
下面将举例说明,当异常抛出后将信息打印在控制台,同时截取当前浏览器窗口,作为后续bug的依据给相应开发人员更好下定位问题。代码如下:
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)
运行有异常,结果如下:
上面代码中用到WebDriver内置的捕获屏幕并保存的方法,如这里的save_screenshot(filename)方法和save_screenshot_as_file(filename)方法,在测试异常抛出时,同时截取浏览器屏幕并以自定义的图片文件名保存在指定路径(上面代码为当前路径)。
又如当一个元素呈现在DOM,但它是不可见的,不能与之进行交互,异常将抛出,以百度首页的登录为例,当元素不能不可见时,抛出ElementNotVisibleException的异常,代码如下:
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)
运行有异常,结果如下:
下面将列举selenium常见的异常:
总结
以上所述是小编给大家介绍的Python 中的Selenium异常处理实例网站的支持!
来源:https://www.cnblogs.com/cnkemi/p/8985654.html
标签:python,异常处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
自动在Windows中运行Python脚本并定时触发功能实现
2023-04-15 21:12:38
![](https://img.aspxhome.com/file/2023/6/76926_0s.png)
[译]Javascript风格要素(一)
2008-02-28 12:58:00
设计哲学与跨界
2009-08-18 12:25:00
Pytorch关于Dataset 的数据处理
2021-04-16 08:40:54
![](https://img.aspxhome.com/file/2023/1/84901_0s.png)
Python设计模式之命令模式原理与用法实例分析
2022-07-28 20:49:15
![](https://img.aspxhome.com/file/2023/4/75314_0s.png)
什么是用户体验优化(UEO)
2008-08-27 19:49:00
php 无法载入mysql扩展
2023-09-07 13:07:40
python保存图片时如何和原图大小一致
2022-07-13 03:34:36
![](https://img.aspxhome.com/file/2023/4/81854_0s.png)
利用sys.sysprocesses检查SqlServer的阻塞和死锁
2011-11-03 17:24:11
javascript面向对象技术基础(四)
2010-02-07 13:15:00
![](https://img.aspxhome.com/file/UploadPic/20102/7/201027131925891s.jpg)
两行代码实现的QQ窗口抖动效果
2008-09-06 12:34:00
最新google pr查询接口
2012-03-12 20:00:39
Python GAE、Django导出Excel的方法
2023-02-27 13:59:09
php通过exif_read_data函数获取图片的exif信息
2023-10-27 09:01:44
Python3中的多行输入问题
2023-02-06 13:11:23
![](https://img.aspxhome.com/file/2023/1/89361_0s.png)
Python协程实践分享
2023-09-01 05:50:51
![](https://img.aspxhome.com/file/2023/5/62875_0s.png)
python转换字符串为摩尔斯电码的方法
2022-01-01 10:17:44
Django中传递参数到URLconf的视图函数中的方法
2021-09-16 23:46:33
python将txt文件读取为字典的示例
2023-02-25 15:57:16
python读取word 中指定位置的表格及表格数据
2021-04-10 02:42:21
![](https://img.aspxhome.com/file/2023/2/99622_0s.png)