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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Auske
  • 37
  • 1
  • 5

1 Answers1

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()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 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 Jul 10 '20 at 17:05
  • @Auske I didn't get your question, can you come again? Was it on `WebDriverWait` or `Keys.Return`? – undetected Selenium Jul 10 '20 at 17:10
  • Sorry, I was asking why not just use `Keys.RETURN`? – Auske Jul 10 '20 at 17:38
  • @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()`. – undetected Selenium Jul 10 '20 at 17:50
  • 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')` – Auske Jul 10 '20 at 18:04
  • @Auske Glad to be able to help you !!! If you have any question on `WebDriverWait` feel free to raise a new question. Stackoverflow contributors will be happy to help you out. – undetected Selenium Jul 10 '20 at 18:21