I'm writing a script to auto fill some fields with Selenium. I'm running into problems where if the script clicks on a button that navigates from the original url, then Python can't find the elements on the new page.
For example: If I have selenium navigate to this landing page first (https://www.emedny.org) and click on the log in for ePaces, and then try to fill out the form, I get an error. This code is what I run:
from selenium import webdriver
browser = webdriver.Chrome(path)
keyed_path = r'https://www.emedny.org'
browser.get(keyed_path)
epaces_login = browser.find_element_by_css_selector('#targetContainer > a:nth-child(3)')
epaces_login.click()
username = browser.find_element_by_id('Username')
username.send_keys('test')
This code opens up https://www.emedny.org, clicks on a button which takes us to a log in page (https://www.emedny.org/epaces), and is supposed to enter in a username on the page. However, I get this error:
NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Username"}
(Session info: chrome=56.0.2924.87)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.3.9600 x86_64)
But, if I skip the landing page and open up the log in page directly, like in this code:
from selenium import webdriver
browser = webdriver.Chrome(path)
keyed_path = r'https://www.emedny.org/epaces'
browser.get(keyed_path)
username = browser.find_element_by_id('Username')
username.send_keys('test')
Then the code runs well, and updates the username field as expected. It seems as though Selenium might be searching for that id on the opening url only.
Here's the HTML for the username field:
<input name="Username" type="text" maxlength="8" id="Username" class="inputText" size="13" onchange="javascript:validate(this)">
Any suggestions? I've tried id, XPath, css selector, name, etc. Same error each time.
EDIT: The link that selenium clicks first opens up a new tab. Could that be part of the issue?
urlusingbrowser.current_url?implicitly waitthe browser. for further reading: selenium-python.readthedocs.io/waits.html