Search code examples
pythonseleniumfindelement

find_element_by_id not working on a specific site but works for Youtube


SOLVED!

I didn't know what iframes were and Arundeep Chohan mentioned them in the comments which lead me down a rabbit hole and know I got it to work.

I would like his answer but he left it as a comment. Thanks Arundeep! ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I am trying to write a program to automatically log me into a site but the find_element_by_id doesn't seem to work for this login page. I got it to work for Youtube but so far the id I am looking for doesn't seem to be detected on this site.

I have also tried find_element_by_name and find_element_by_class to no avail either.

Below is my code

# Imports
import selenium
from selenium import webdriver

# Variables
UsNam = "MY_USERNAME"

# Assigning Firefox to browser
browser = webdriver.Firefox(executable_path=r"C:\Users\ME\Geckodriver\geckodriver-v0.27.0-win64\geckodriver.exe")

# Opening the webpage
browser.get("https://THESITEIWANTTOLOGINTO.com/abunchofloginpagestuff")

# Finding the login input
NameEntry = browser.find_element_by_id('USER')
browser.implicitly_wait(15)

# Entering the username
NameEntry.send_keys(UsNam)


Here is the element that I can't seem to find in selenium,

<input class="form-control ng-pristine ng-invalid ng-invalid-required" type="text" name="USER" id="USER" data-payxautoid="paychex.app.login.userName.input.username" placeholder="Enter Username" maxlength="50" data-ng-model="user.username" data-payx-focus="" data-ng-change="clearShowError()" required="" data-payx-form-value="siteminder.username">

One thing that is weird that I just noticed is when I copy and paste the element the id doesn't appear there. It should read "input id="USER" class=..."

Any help would be greatly appreciated. I tried reading the other Overflow questions and didn't find any answers which is why I am here now

Thanks

EDIT: Here is my error message

Traceback (most recent call last):
  File "C:/Users/Crow/PycharmProjects/Login/main.py", line 15, in <module>
    NameEntry = browser.find_element_by_id('USER')
  File "C:\Users\Crow\PycharmProjects\Login\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\Crow\PycharmProjects\Login\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Crow\PycharmProjects\Login\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Crow\PycharmProjects\Login\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="USER"]

Solution

  • After driver.get()

    driver.implicitly_wait(10) 
    

    Then do the search. You could use webdriver waits instead.

    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
    

    Inspect the page and check your iframes. Copy what the xpath,css,id of your iframe is and insert it into the above line and change accordingly.

    Instead of wait and search:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "User"))).send_keys(UsNam)
    

    Import

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