0

I have a program which uses the python selenium webdriver and I get the following runtime error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id='id_login']"}
  (Session info: chrome=83.0.4103.1

HTML

<input type="text" name="login" placeholder="Type your username" required="" id="id_login" xpath="1">16)

code:

from selenium import webdriver
#from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path="c:\\Chrome1\chromedriver.exe")
driver.get("https://www.jstor.org")
print(driver.title)

driver.find_element_by_xpath("//a[@class='inline-block plm']").click()
driver.find_element_by_xpath("//input[@id='id_login']").send_keys('xxxxxx@gmail.com')
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Is the error showing for the element with xpath //a[@class='inline-block plm'] also or only for the input with id id_login? – Ali Rida Jul 20 '20 at 07:09

3 Answers3

0

This error means that selenium could not localize the element because it was not on the site or it did not load. I suggest you using the WebdriverWait() function. It will wait X seconds until the element is clickable. If it will still not be, it will throw an error.

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

WebDriverWait(self.webdriver, 60).until(EC.element_to_be_clickable((By.XPATH, '//a[@class='inline-block plm']')))
Aleksander Ikleiw
  • 2,104
  • 1
  • 4
  • 20
0

More effective if you direct directly to this url:

https://www.jstor.org/action/showLogin?redirectUri=/

And use selenium wait to solve your issue.

driver.get('https://www.jstor.org/action/showLogin?redirectUri=/')
user_name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='id_login']")))
user_name.send_keys('xxxxxx@gmail.com')

Although your xpath will work, using an id looks better .element_to_be_clickable((By.ID, "id_login"))

You need following import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
frianH
  • 5,901
  • 6
  • 13
  • 36
0

To send a character sequence to the Username field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://www.jstor.org/")
    ebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.inline-block.plm"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id_login"))).send_keys("SunilTirupathi@stackoverflow.com")
    
  • Using XPATH:

    driver.get("https://www.jstor.org/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='inline-block plm']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='id_login']"))).send_keys("SunilTirupathi@stackoverflow.com")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

jstor


References

You can find a couple of relevant discussions on NoSuchElementException in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217