简体   繁体   中英

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. My task is to set the value for text box name "rcdate" for the mention URL by using selenium. 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

<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

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:

  • CSS_SELECTOR :

     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")

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",

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM