Reputation: 37
I need to log into a website and I already have the following code, which works fine:
username = driver.find_element_by_name('uname')
password = driver.find_element_by_name('password')
username.send_keys('username'), password.send_keys('password', Keys.RETURN)
Right now, the username and password are hard-coded into the program, but I will eventually require the user to supply both for security reasons. Is there a way to make what I have written more compact or more Pythonic? I'm trying to stay as close to PEP8 as best I can for my own benefit.
Upvotes: 2
Views: 44
Reputation: 193208
Your program is completely Pythonic unless there are runtime errors.
Having said that, from Selenium point of view there are a couple of concerns as follows:
Presumably, you will initiate locating the username after invoking get()
. So ideally you have to induce WebDriverWait for the element_to_be_clickable()
and invoke send_keys()
thereafter as follows:
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "'uname'"))).send_keys('username')
driver.find_element_by_name('password').send_keys('password')
Moreover, I would suggest to avoid an attempt to send_keys(Keys.RETURN)
unless you are left out with any other option. Ideally, you should invoke click()
on the Login or Sign In button as follows:
driver.find_element_by_css_selector('cssSelector').click()
# incase the elements are with in a <form>
driver.find_element_by_css_selector('cssSelector').submit()
Upvotes: 1