Home > Software engineering >  Why does the selenium send keys function not always work for my code?
Why does the selenium send keys function not always work for my code?

Time:10-19

The following code:

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


def github_login(username, password, github_username):
    browser = webdriver.Chrome()
    browser.get('https://github.com')

    browser.find_element(By.XPATH, '/html/body/div[1]/header/div/div[2]/div/div/div[2]/a').click()

    username_box = browser.find_element(By.ID, 'login_field')
    username_box.send_keys(username   Keys.RETURN)

    password_box = browser.find_element(By.ID, 'password')
    password_box.send_keys(password   Keys.RETURN)

    browser.find_element(By.XPATH, '/html/body/div[1]/header/div[7]/details/summary/img').click()
    browser.find_element(By.XPATH, '/html/body/div[1]/header/div[7]/details/details-menu/a[1]').click

    if browser.current_url == 'https://github.com/'   str(github_username):
        print('successfully loged in to github')
        return 'successfull login'
    else:
        print('Login failed')
        return 'login failed'


login = github_login(username='code7g', password='randompassword', github_username='Code7G')

executes correctly only 1/10 time.

From line 0 to 10:

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


def github_login(username, password, github_username):
    browser = webdriver.Chrome()
    browser.get('https://github.com')

    browser.find_element(By.XPATH,'/html/body/div[1]/header/div/div[2]/div/div/div[2]/a').click()

the browser does everything correctly 100% of the time.

But when it comes to the send keys function:

username_box = browser.find_element(By.ID, 'login_field')
username_box.send_keys(username   Keys.RETURN)

password_box = browser.find_element(By.ID, 'password')
password_box.send_keys(password   Keys.RETURN)

the browser does not send the keys 90% of the time, sometimes it does and I don't know why. Does this have to do something with the GitHub login system or with my code?

CodePudding user response:

There are several issues I can point here:

  1. Looks like you are missing delays.
    You should wait for elements to be clickable or visible before clicking them or sending them texts. WebDriverWait expected_conditions explicit conditions should be used for that.
  2. You should improve your locators. Absolute XPaths are extremely unreliable and fragile.
  3. No need to send Keys.RETURN with username. Also, I'd prefer clicking "Sing in" button rather than sending Keys.RETURN with password, but if it works - no problem :)
    This should work better:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

def github_login(username, password, github_username):
    browser = webdriver.Chrome()
    wait = WebDriverWait(browser, 10)
    browser.get('https://github.com')
    wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@href='/login']"))).click()
    wait.until(EC.element_to_be_clickable((By.ID, "login_field"))).send_keys(username)
    wait.until(EC.element_to_be_clickable((By.ID, "password"))).send_keys(password)
    wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@name='commit']"))).click()
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "summary .avatar-small"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Your profile')]"))).click()

  • Related