按照虫师著作的《Selenium2 Python自动化测试实战(第二版)》中的以下代码,s实现的功能是打开网页并验证是否是正确的网页。我在sublimeTest中运行,使用的是Chrome浏览器:
运行后出错:
E
======================================================================
ERROR: test_youdao (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "E:\learn\Selenium2_Python_test\test_project\test_case\test_youdao.py", line 14, in test_youdao
driver.find_element_by_id("query").clear()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"query"}
(Session info: chrome=67.0.3396.79)
(Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.17134 x86_64)
竟然定位不到?!
于是本着学习的态度,用firebug查看了有道的查询定位元素,一看不知道,看了才发现定位方式都变了
然后改了新的定位方式之后,再次运行,通过了! 新手的我要吸取教训啊,网页更新那么快,按照别人代码学习的时候,要看最新的网页的格式,否则还会遇到同样的问题!