2

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.

1 Answer 1

1

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()
    
Sign up to request clarification or add additional context in comments.

6 Comments

I like the idea of using WebDriverWait before getting the username element, but what is the reasoning behind finding the sign in button versus just using Keys.Return? I'm still learning a lot about Python through this project, which is rather intimidating because I'm doing it for my work even though this is not at all what I was hired for. But me doing this will increase my and one of my coworker's productivity.
@Auske I didn't get your question, can you come again? Was it on WebDriverWait or Keys.Return?
Sorry, I was asking why not just use Keys.RETURN?
@Auske One of the main reason is the Login or Sign In button can be Javascript enabled and even can have onclick events which will only fire with invoking click().
I just tried to use WebDriverWait in my code, but I couldn't get it to work. It would timeout trying to find the element. But I did go ahead and change Keys.RETURN. Here are my new lines: print('Finding username and password and sending both') driver.find_element_by_name('uname').send_keys(username) driver.find_element_by_name('password').send_keys(password) driver.find_element_by_class_name("bttn").click() print('Logging in')
|

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.