Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用selenium在文本字段中设置值。selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:_Python_Selenium_Selenium Webdriver_Css Selectors_Webdriverwait - Fatal编程技术网

Python 使用selenium在文本字段中设置值。selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:

Python 使用selenium在文本字段中设置值。selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:,python,selenium,selenium-webdriver,css-selectors,webdriverwait,Python,Selenium,Selenium Webdriver,Css Selectors,Webdriverwait,我不熟悉网络抓取和python。我的任务是使用selenium为提及URL的文本框名称“rcdate”设置值。然后刮取已过滤的值。当它运行时,会出现此异常。这是我尝试运行的代码 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.suppor

我不熟悉网络抓取和python。我的任务是使用selenium为提及URL的文本框名称“rcdate”设置值。然后刮取已过滤的值。当它运行时,会出现此异常。这是我尝试运行的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 


browser = webdriver.Firefox()
browser.get("http://www.irrigation.gov.lk/index.php?option=com_reservoirdata&Itemid=255&lang=en")

wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, 'rcdate')))

browser.find_element_by_tag_name("rcdate").send_keys("2018-10-01")
那么错误消息是

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: rcdate
Html


我在尝试访问url时遇到错误403-禁止
http://www.irrigation.gov.lk/index.php?option=com_reservoirdata&Itemid=255&lang=en

但是,一旦找到元素并在尝试调用
send\u keys()
方法时向前移动,则应使用
元素可点击()
而不是预期的条件,如下所示:

  • CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#rcdate"))).send_keys("2018-10-01")
    
  • ID

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "rcdate"))).send_keys("2018-10-01")
    
使用

find_element_by_name() # locate element by the "name" attribute
不可混淆

find_element_by_tag_name() # locate element by the element tag ("input" in this case)

你在这条线上做错了,所以 用这条线,

browser.find_element_by_tag_name("rcdate").send_keys("2018-10-01")
没有意义,因为没有这样的标签,您使用的是标签名“rcdate”

使用

browser.find_element_by_id("rcdate").send_keys("2018-10-01")
browser.find_element_by_name("rcdate").send_keys("2018-10-01")

我希望这能对你有所帮助

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 


browser = webdriver.Firefox()
browser.get("http://www.irrigation.gov.lk/index.php?option=com_reservoirdata&Itemid=255&lang=en")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#rcdate"))).send_keys("2018-10-01")

发布相关html。文本框html
是否有任何名为“rcdate”的标记,如果没有,则不要使用行浏览器。通过标记名(“rcdate”)查找元素。发送键(“2018-10-01”)是否有任何方法替换现有文本框值。然后我需要从代码中传递值。虽然这段代码可能有效,但所描述的解决方案与实际问题无关:它不是等待元素可单击,而是错误的选择器策略,所以其他两个答案似乎更有效useful@Haz例如,可以…将现有文本框值替换为新值。。。使用execute_script()方法。请随时提出新问题和新要求。投稿人会很乐意帮助你。@DebanjanB是的,当然。谢谢你谢谢Hanks@Ashish的帮助。它起作用了。我需要替换现有的文本框值。然后我需要从代码中传递值。这有什么机制吗。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 


browser = webdriver.Firefox()
browser.get("http://www.irrigation.gov.lk/index.php?option=com_reservoirdata&Itemid=255&lang=en")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#rcdate"))).send_keys("2018-10-01")