成长的喵星人 发表于 2016-5-17 11:27:50

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"}
有人知道为什么吗?急急急


掉渣饼 发表于 2016-5-17 13:39:27

在driver.get("https://mail.qq.com/")下面加上
time.sleep(5)
driver.switch_to_frame("login_frame")

liiiih 发表于 2016-5-17 13:29:41

wait 试试呢,

成长的喵星人 发表于 2016-5-17 18:03:41

掉渣饼 发表于 2016-5-17 13:39
在driver.get("https://mail.qq.com/")下面加上
time.sleep(5)
driver.switch_to_frame("login_frame")

可以运行了
页: [1]
查看完整版本: python+selenium 定位error