0

The below is a selenium python code where I am trying to click Sign In by sending the login details via selenium. However, when I am using find_element_by_id method to locate the username and password input area the scripts throws an error Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="usernameOrEmail"]"}. But, when I am inspect the webpage on the input text type it shows me the same id which I have mentioned in my script.

P.S: When the selenium opens up the browser please maximize the windows else the code will not work

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path='C://Arighna/chromedriver.exe')
driver.get("https://www.fool.com/")
print(driver.title)
mybutton = driver.find_element_by_id('login-menu-item')
mybutton.click()
delay = 5
WebDriverWait(driver,delay)
email_area = driver.find_element_by_id('usernameOrEmail')
email.send_keys(Keys.ENTER)
email_area.send_keys('ar')
WebDriverWait(driver,delay)

pwd_area = driver.find_element_by_id('password')
pwd_area.send_keys(Keys.ENTER)
pwd_area.send_keys('1234')
WebDriverWait(driver,delay)


login_btn = driver.find_element_by_id('btn-login')
login_btn.click()

Any help is really appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Souradip Roy
  • 129
  • 1
  • 1
  • 7

2 Answers2

0

I modified the above code as this one and it worked.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path='C://Arighna//chromedriver.exe')
driver.get("https://www.fool.com/")
mybutton = driver.find_element_by_id('login-menu-item')
mybutton.click()


WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[@id='usernameOrEmail']"))
)
driver.find_element_by_xpath("//input[@id='usernameOrEmail']").send_keys("Selenium")


WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[@id='password']"))
)
driver.find_element_by_xpath("//input[@id='password']").send_keys("Selenium")

singin_bttn = driver.find_element_by_id('btn-login')
singin_bttn.click()
Souradip Roy
  • 129
  • 1
  • 1
  • 7
0

To send a character sequence to the Email / Username field you need 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.fool.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#login-menu-item"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#usernameOrEmail"))).send_keys("SouradipRoy")
    
  • Using XPATH:

    driver.get('https://www.fool.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='login-menu-item']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='usernameOrEmail']"))).send_keys("SouradipRoy")
    
  • 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:

fool


References

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352