这篇文章主要介绍了关于PHPPython 中的Selenium异常处理,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
自动化测试执行过程中,难免会有错误/异常出现,比如测试脚本没有发现对应元素,则会立刻抛出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异常处理的详细内容。更多信息请关注PHP中文网其他相关文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3汉化版
中文版,非常好用