51Testing软件测试论坛

标题: python+selenium 定位error [打印本页]

作者: 成长的喵星人    时间: 2016-5-17 11:27
标题: python+selenium 定位error
同一个元素,用不同的方法定位,有时候会报错是为什么?
元素:
<input class="inputstyle" id="u" name="u" value="" tabindex="1" type="text">
代码:
# coding = utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://mail.qq.com/")
driver.find_element_by_id("u").clear()
driver.find_element_by_id("u").send_keys("12345678")

运行后报错:Traceback (most recent call last):
  File "C:\Python34\2222222222.py", line 7, in <module>
    driver.find_element_by_id("u").clear()
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 266, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 744, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"u"}
有人知道为什么吗?急急急



作者: liiiih    时间: 2016-5-17 13:29
wait 试试呢,
作者: 掉渣饼    时间: 2016-5-17 13:39
在driver.get("https://mail.qq.com/")下面加上
time.sleep(5)
driver.switch_to_frame("login_frame")

作者: 成长的喵星人    时间: 2016-5-17 18:03
掉渣饼 发表于 2016-5-17 13:39
在driver.get("https://mail.qq.com/")下面加上
time.sleep(5)
driver.switch_to_frame("login_frame")

可以运行了




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2