C:\apache-tomcat-8.0.27>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> webdriver.__version__
'3.0.2'
>>>
selenium python测试代码如下:
driver.get("http://localhost:8080/")
self.assertEqual("Cubiender", driver.title)
driver.find_element_by_id("login_email").clear()
driver.find_element_by_id("login_email").send_keys("gin@cubi.com")
driver.find_element_by_id("login_pwd").clear()
driver.find_element_by_id("login_pwd").send_keys("pass")
driver.find_element_by_css_selector("input[type=\"submit\"]").click()
driver.find_element_by_link_text('Project List')
sleep(0.05)
driver.find_element_by_xpath("//input[@value='2588']").click()
sleep(0.05)
driver.find_element_by_css_selector("div.menu > #inquireProject").click()
上面的代码可以在调试模式下成功运行,但在运行时会失败 driver.find_element_by_xpath(" //输入[@value =' 2588']&#34)。单击()
即使我加入了睡眠
堆栈跟踪:
C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pycharm\utrunner.py" F:\python\sub_proj2.py true
测试于10:53开始......
Error
Traceback (most recent call last):
File "F:\python\sub_proj2.py", line 41, in test_untitled
driver.find_element_by_css_selector("span.triangle").click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 437, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException:消息:无法找到元素:span.triangle
处理完成,退出代码为0
答案 0 :(得分:2)
请分享堆栈跟踪。
似乎对我来说,
value='2588
是动态的,并随着每个实例加载而变化。sleep
停止整个过程。为什么不使用wait
,这是下面提到的一个例子。 from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
答案 1 :(得分:0)
这可能是因为在调试模式下,它有更多的时间来查找元素。尝试增加睡眠时间或其他解决方案以确保元素已加载。