带有硒的 Python:无法定位真正存在的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24369249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python with selenium: unable to locate element which really exist
提问by ti01878
I've been trying fill input:
我一直在尝试填充输入:
<input id="PASSFIELD1" class="logField" type="password" onkeyup="next(this, event);" maxlength="1" autocomplete="off" name="PASSFIELD1"></input>
To do this, I have to find this element.
为此,我必须找到这个元素。
I tried below things:
我尝试了以下事情:
pass1=driver.find_element_by_name("PASSFIELD1")
pass1=driver.find_element_by_id("PASSFIELD1")
pass1= driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]")
(path from firebug)Even wait 100 seconds for it
pass1=driver.find_element_by_name("PASSFIELD1")
pass1=driver.find_element_by_id("PASSFIELD1")
pass1= driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]")
(来自萤火虫的路径)甚至等待 100 秒
self.wait.until(EC.visibility_of_element_located((By.XPATH,"/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]")))
self.assertTrue(self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]"))
self.wait.until(EC.visibility_of_element_located((By.XPATH,"/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]")))
self.assertTrue(self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div/form/div[3]/table/tbody/tr[3]/td[2]/div/input[1]"))
I always get:
我总是得到:
selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate element: (...)
selenium.common.exceptions.NoSuchElementException:消息:'无法定位元素:(...)
Do you know what I am doing wrong?
你知道我做错了什么吗?
采纳答案by alecxe
回答by Vikram Rayavarapu
Add some delay to the driver so that elements will load.
向驱动程序添加一些延迟,以便加载元素。
import time
time.sleep(2)
department_element = driver.find_elements_by_id("__id_name__")