Gus Gabel Gus Gabel - 2 years ago 147
Python Question

Python Selenium: Can't find the login element of

I am getting very frustrated trying to login to with python and selenium. I have tried it on the chrome browser and firefox browser, but can't figure out the correct code.

I have logged into other websites, but cannot figure out how to input text into the login boxes for this site.

Here's what I have so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome(executable_path="C:/Users/Gus Gabel/Anaconda/chromedriver.exe")


#the username and password boxes are hidden until you press the login button on the home page
login = browser.find_element_by_id("loginLink")

#now that the username and password boxes are available, I've tried finding the elements by class_name, xpath, (BY.NAME), id, etc and nothing has worked
#here are a few codes that haven't worked and the errors associated
user = browser.find_element_by_id('username')
NoSuchElementException: Message: no such element

user = browser.find_element_by_class_name('usernameNew')
NoSuchElementException: Message: no such element

#when i try to use the above code with "elements" instead of "element", no error message pops up.
user = browser.find_elements_by_class_name('usernameNew')

#but then whey i try to choose which element by doing this
user = browser.find_element_by_class_name('usernameNew')[0]
NoSuchElementException: Message: no such element

How is it possible that there can be a list of elements, but yet not have an initial element?

If anyone can figure out how to input text into the username and password text boxes of, I will be greatly appreciative. I can't figure this out for the life of me!

Answer Source

To enter text into an input box with Selenium (e.g, your user name into the username field), use the send_keys method of the element:


You can also try Selenium's action_chains module, as in the following code (untested), to get past the roadblock from @dm295's answer:

from selenium import webdriver
from selenium.webdriver.common import action_chains

driver = webdriver.Firefox()

action = action_chains.ActionChains(driver)

login = driver.find_element_by_class_name("login-icon")


username = driver.find_element_by_id('username')


username.send_keys('Gus Gabel')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download