测试积点老人 发表于 2018-4-19 09:52:01

Day5-7测试积点任务

问题:
元素不能定位的时候 直接退出报错,is_display() 这样的函数怎么使用?
<font face="微软雅黑" size="3">def test_findkeyboard(self):
    el = self.driver.find_element_by_class_name('UIAKeyboard')
    print(el.is_displayed())
执行以上test的时候直接报错
An element could not be located on the page using the given search parameters.</font>



求助下 is_display()这样的函数应该怎么使用


<font face="微软雅黑" size="3">/usr/bin/python "/Applications/PyCharm CE.app/helpers/pycharm/utrunner.py" /Users/Alfred/sample-code-master/sample-code/examples/python/sapi_login_test.py::SapiHomePageTests::test_findkeyboard true
Testing started at 下午5:57 ...

Error
Traceback (most recent call last):
File "/Users/Alfred/sample-code-master/sample-code/examples/python/sapi_login_test.py", line 44, in test_findkeyboard
    el = self.driver.find_element_by_class_name('UIAKeyboard')
File "/Library/Python/2.7/site-packages/selenium-2.41.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 343, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
File "/Library/Python/2.7/site-packages/selenium-2.41.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 655, in find_element
    {'using': by, 'value': value})['value']
File "/Library/Python/2.7/site-packages/selenium-2.41.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 166, in execute
    self.error_handler.check_response(response)
File "build/bdist.macosx-10.9-intel/egg/appium/webdriver/errorhandler.py", line 29, in check_response
    raise wde
NoSuchElementException: Message: u'An element could not be located on the page using the given search parameters.' </font>


abcsell 发表于 2018-4-20 10:03:32

查找出错的位置,然后看看上下文的引用

梦想家 发表于 2018-4-20 10:07:56

第一个问题:没有找到元素而导致脚本退出,可以使用Try Catch来捕获异常,从而可以继续下面的测试
第二个问题: is_displayed()应该就是这样使用的,可以判断元素对用户是否可见,但是根据你的报错信息,这个元素未找见并导致脚本执行失败,所以el.is_displayed()自然是不会执行

海海豚 发表于 2018-4-20 10:18:41

https://testerhome.com/topics/1594?locale=en可以做一下参考

qqq911 发表于 2018-4-20 10:29:16

is_displayed() 是布尔函数,只是返回true or false

jingzizx 发表于 2018-4-20 10:58:18

判断下元素是否出来再进行后续操作
页: [1]
查看完整版本: Day5-7测试积点任务