Selenium No Element Found Exception even after copying element from source code
Selenium No Element Found Exception even after copying element from source code
I am trying to simply login to this page to access LexisNexis. Here is my code:
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
type(browser)
browser.get('https://sfx.carli.illinois.edu/sfxuiu?url_ver=Z39.88-2004&url_ctx_fmt=infofi/fmt:kev:mtx:ctx&ctx_enc=info:ofi/enc:UTF-8&ctx_ver=Z39.88-2004&rfr_id=info:sid/sfxit.com:azlist&sfx.ignore_date_threshold=1&rft.object_id=63750000000001351&svc.fulltext=yes')
linkElem2 = browser.find_element_by_link_text('LEXIS NEXIS DATABASES')
type(linkElem2)
linkElem2.click()
alert = browser.switch_to.alert
alert.accept()
loginElem = browser.find_element_by_id('j_username')
loginElem.send_keys('username')
passElem = browser.find_element_by_id('j_password')
passElem.send_keys('password')
passElem.submit()
And here is the html source:
I am not sure what I am doing wrong :/
2 Answers
2
1. Problem
"LEXIS NEXIS DATABASES" link open new window and you have to switch to it to login, Handle multiple windows in Python:
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
browser.implicitly_wait(5)
browser.get('https://sfx.carli.illinois.edu/sfxuiu?url_ver=Z39.88-2004&url_ctx_fmt=infofi/fmt:kev:mtx:ctx&ctx_enc=info:ofi/enc:UTF-8&ctx_ver=Z39.88-2004&rfr_id=info:sid/sfxit.com:azlist&sfx.ignore_date_threshold=1&rft.object_id=63750000000001351&svc.fulltext=yes')
browser.find_element_by_link_text('LEXIS NEXIS DATABASES').click()
browser.close()
browser.switch_to.window(browser.window_handles[0])
browser.find_element_by_id('j_username').send_keys('username')
passElem = browser.find_element_by_id('j_password')
passElem.send_keys('password')
passElem.submit()
2. Problem Link you share in comments is have error when you open for the first time(new browser profile).
All you need is open page and click "Login with your Illinois NetID" to open login page successfully. This way much better, because you don't need to handle windows, check code below:
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
browser.implicitly_wait(5)
browser.get('https://compass2g.illinois.edu/webapps/login/')
browser.find_element_by_link_text('Login with your Illinois NetID').click()
browser.implicitly_wait(5)
browser.find_element_by_id('j_username').send_keys('username')
passElem = browser.find_element_by_id('j_password')
passElem.send_keys('password')
passElem.submit()
Provide url or html
– sers
Aug 23 at 20:34
Check if inputs inside iframe tag
– sers
Aug 23 at 20:35
shibboleth.illinois.edu/idp/profile/SAML2/POST/…
– D. Shaat
Aug 23 at 21:44
See my updated answer
– sers
Aug 23 at 23:15
Clicking on that link opens a new tab, so you need to switch to new tab before locating username and password fields. Try this (indentation may be broken):
from selenium import webdriver
import time
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(executable_path='/usr/local/bin/geckodriver')
browser.get('https://sfx.carli.illinois.edu/sfxuiu?url_ver=Z39.88-2004&url_ctx_fmt=infofi/fmt:kev:mtx:ctx&ctx_enc=info:ofi/enc:UTF-8&ctx_ver=Z39.88-2004&rfr_id=info:sid/sfxit.com:azlist&sfx.ignore_date_threshold=1&rft.object_id=63750000000001351&svc.fulltext=yes')
linkElem2 = browser.find_element_by_link_text('LEXIS NEXIS DATABASES')
linkElem2.click()
alert = browser.switch_to.alert
alert.accept()
time.sleep(5) #to add 5 sec wait, you can use other wait methods for window handle. e.g. https://stackoverflow.com/questions/26641779/python-selenium-how-to-wait-for-new-window-opens
browser.switch_to.window(browser.window_handles[1])
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'j_username')))
loginElem = browser.find_element_by_id('j_username')
loginElem.send_keys('username')
passElem = browser.find_element_by_id('j_password')
passElem.send_keys('password')
passElem.submit()
you will need following imports
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
Thank you! I am still getting errors though. first it said name 'driver' not defined, so I used browser instead. Then it said list index out of range, so I changed it to 0. Then it says name 'wait' not defined, so I used:
– D. Shaat
Aug 23 at 18:18
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
– D. Shaat
Aug 23 at 18:18
correct, You will need imports for wait method, please see my updated answer
– theGuy
Aug 23 at 18:23
I used the imports, but I keep getting Timeout Exception. I tried 10,20,30,60 but still not working :/
– D. Shaat
Aug 23 at 18:26
I am not sure if this means it still can't find the element. Maybe the element is hidden? But I am not sure how to make it so that python can find it!!
– D. Shaat
Aug 23 at 18:27
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Still not working :/ I even ran the exact code from the book automate the boring stuff, the one to login to email, and it gives the same error! Element not found!!
– D. Shaat
Aug 23 at 20:32