1

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?

4
  • have you tried finding the current url using browser.current_url? Commented May 31, 2017 at 19:00
  • conversely, you might need to implicitly wait the browser. for further reading: selenium-python.readthedocs.io/waits.html Commented May 31, 2017 at 19:01
  • @SreetamDas I didn't use current_url because I thought that was just a way to access the url as a string. I'll try the wait methods. Thank you Commented May 31, 2017 at 19:04
  • @SreetamDas I tried both explicit and implicit waits for 45 seconds, both timed out. Commented May 31, 2017 at 19:12

1 Answer 1

2

I figured it out. It's because the link being clicked on opened up a new tab. Selenium was searching for the elements on the previous tab. I used window_handles and switch_to_window like this:

browser = webdriver.Chrome(path)


keyed_path = r'https://www.emedny.org'
browser.get(keyed_path)

print(browser.window_handles)

epaces_login = browser.find_element_by_css_selector('#targetContainer > a:nth-child(3)')
epaces_login.click()


browser.switch_to_window(browser.window_handles[1])

username = browser.find_element_by_id('Username')

username.send_keys('test')
Sign up to request clarification or add additional context in comments.

3 Comments

just a suggestion @jaredstuft, try using Selenium; it's much more lightweight, and you wouldn't have faced the new-tab error in the first place using it :)
@TheDarkKnight I was using Selenium... are you seeing something different? It's a very new library for me so I'm not sure about best practices.
Hmm, how about using PhantomJS instead of the Chrome driver then? It is a headless driver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.