4

I try to select my input with selenium but when I use this it doesn't work:

driver = self.driver
password = driver.find_element_by_xpath("//input[@name='password']")
password.clear()
password.send_keys(password)
password.send_keys(Keys.RETURN)

# the sentence below doesn't work
password.send_keys(Keys.COMMAND, 'a')
password.send_keys(Keys.DELETE)

I am using Mac so Keys.CONTROL doesn't work, can anyone help me how to select the input or how to clear it?

Thanks

5
  • Is the page loading after password.send_keys(Keys.RETURN)? Commented Jan 7, 2020 at 20:14
  • How are you getting password_alem? Can you add more of your code? Commented Jan 7, 2020 at 20:15
  • yes, but not if the password is wrong Commented Jan 7, 2020 at 20:15
  • @Jortega sorry it was a copy mistake Commented Jan 7, 2020 at 20:16
  • See the answer below and remember to mark it if it solves your issue. See: stackoverflow.com/help/someone-answers Commented Jan 7, 2020 at 20:17

2 Answers 2

5

You have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']")))
    password.click()        
    password.clear()
    password.send_keys("Tijmen")
    
  • Using XPATH:

    password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
    password.click()
    password.clear()
    password.send_keys("Tijmen")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Reference

You can find a couple of relevant discussions in:

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your code, but it only clicks on the input field, it doesn't select it..?
click() and clear() back to back should do the job. If you are observing a different behavior can you update the question with the relevant HTML?
Okay, so I am trying to login into instagram, because I have a list with all my passwords and I always forget my passwords so I created a script were I try to test every password, if it doesn't work I want my script to clear the old input and try a new password. but it still doesn't work...
2

Mac cannot use COMMAND you need Keys.BACKSPACE Try:

driver = self.driver
password = driver.find_element_by_xpath("//input[@name='password']")
password.clear()
password.send_keys(password)
password.send_keys(Keys.RETURN)
#password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
#or
#time.sleep(1)
for i in range(len(password)):
    password.send_keys(Keys.BACKSPACE)

1 Comment

thank, I tied this before, but the delay is too long, is there a faster way?

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.