0

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"]
BirdBud
  • 143
  • 1
  • 15

2 Answers2

1

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
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

I can think of 2 solutions:

  1. Make the browser implicitly wait before searching for the element, as to give time for the website to load.
  2. Use xpath instead of finding by id name, as simple as right clicking the element in inspect element and clicking copy full xpath.
kev1n
  • 140
  • 1
  • 7
  • I moved the implicit wait to before the find element and set it to 15. It still returns the error message I have added. Could you elaborate on how to use the xpath? What method would I use to implement that? – BirdBud Sep 26 '20 at 00:08
  • browser.find_element_by_xpath('YOUR XPATH HERE') – kev1n Sep 26 '20 at 00:12
  • I am still getting an error message ``` Message: Unable to locate element: //*[@id="USER"] ``` I have the xpath as //*[@id="USER"] – BirdBud Sep 26 '20 at 00:27
  • You aren't copying the xpath from the website correctly. Right click on the part of the page, inspect element, right click the part of the html, and press copy either (both should work) full xpath or regular xpath (on chrome at least) and then put it into the find_element_by_xpath command. – kev1n Sep 26 '20 at 02:01
  • I am using firefox. I am right clicking the login box, then clicking inspect element, I am right clicking the inspected element and then clicking copy and then clicking Xpath. What should it look like? – BirdBud Sep 26 '20 at 02:27
  • If that xpath didn't work, try the option that says copy full xpath, which should look something along the lines of /html/body/div[4]/div[2]/div/div[1] – kev1n Sep 26 '20 at 02:48
  • That is the only option with xpath for me to copy from firefox. another person mentioned iframes and it looks like what I am trying to get to is contained within that iframe. I am going to try and get around that. – BirdBud Sep 26 '20 at 02:51