[英]Setting a value in a text field using selenium. selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:
I'm new to the Web scraping stuff and python.我是 Web 抓取工具和 Python 的新手。 My task is to set the value for text box name "rcdate" for the mention URL by using selenium.我的任务是使用 selenium 为提及 URL 设置文本框名称“rcdate”的值。 Then scrape the values which were filtered.然后刮掉被过滤的值。 when it's run it gives this exception.当它运行时,它给出了这个异常。 This is the code which I try to run这是我尝试运行的代码
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")
Then Error Msg is然后错误消息是
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: rcdate
Html html
<input type="text" name="rcdate" id="rcdate" value="2018-10-11">
I get an ERROR 403 - FORBIDDEN while trying to access the url http://www.irrigation.gov.lk/index.php?option=com_reservoirdata&Itemid=255&lang=en
我在尝试访问 url http://www.irrigation.gov.lk/index.php?option=com_reservoirdata&Itemid=255&lang=en
时收到错误 403 - FORBIDDEN
However, once you locate the element and moving forward as you are trying to invoke send_keys()
method, so instead of expected_conditions as presence_of_element_located()
you should use element_to_be_clickable()
as follows:但是,一旦您在尝试调用send_keys()
方法时找到元素并向前移动,那么您应该使用element_to_be_clickable()
代替 expected_conditions 作为presence_of_element_located()
element_to_be_clickable()
,如下所示:
CSS_SELECTOR
: CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#rcdate"))).send_keys("2018-10-01")
ID
: ID
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "rcdate"))).send_keys("2018-10-01")
Use用
find_element_by_name() # locate element by the "name" attribute
Not to be confused with不要混淆
find_element_by_tag_name() # locate element by the element tag ("input" in this case)
You are doing wrong at this line, So using this line,你在这条线上做错了,所以使用这条线,
browser.find_element_by_tag_name("rcdate").send_keys("2018-10-01")
make no sense, as there is no such tag present and you are using by tag name "rcdate",没有意义,因为不存在这样的标签,并且您正在使用标签名称“rcdate”,
use either,使用,
browser.find_element_by_id("rcdate").send_keys("2018-10-01")
browser.find_element_by_name("rcdate").send_keys("2018-10-01")
or或者
I hope this might help you,我希望这可以帮助你
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.