Gus Gabel Gus Gabel - 6 months ago 36
Python Question

Python Selenium: Can't find the login element of costar.com

I am getting very frustrated trying to login to costar.com 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")
browser.get("http://www.costar.com")

browser.maximize_window()

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

#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 costar.com, I will be greatly appreciative. I can't figure this out for the life of me!

Answer

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:

input_element.send_keys('some_text_string')

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()
driver.maximize_window()
driver.get("http://www.costar.com")

action = action_chains.ActionChains(driver)

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

driver.switch_to.frame(driver.find_element_by_id('custlogin'))

username = driver.find_element_by_id('username')

action.move_to_element(username).perform()

username.send_keys('Gus Gabel')
Comments