简体   繁体   中英

Chromedriver + Selenium cannot send 's' key

Using Python 3.7.4, Selenium 3.141.0, Chromedriver 78 and Chrome 78.

I'm am trying to automate login to a webpage but when I send keys to the inputs in the form it sends all the characters but the 's' letter.

input = form_element.find_element_by_xpath(password_xpath)
input.send_keys("password")

It only writes "paword". I've seen this issue in other querstions but the Chromedriver version was old. Also I tried with "\s" but it writes "\".

Didn't find any documentation of this. I tried with Chrome 77 + Chromedriver 77, same result. Have anyone had this problem before? What can I do?

When you are trying to identify the password field and subsequently send a character sequence , possibly some JavaScript or AJAX call is in progress. Hence the issue.


Solution

To locate and send a character sequence to the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "password_css"))).send_keys("password")
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "password_xpath"))).send_keys("password")
  • 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

You could wrap send_keys in a method to slowly send keys with a wait between each character press. This might help slow down the key send and allow any Javascript events on the field to fire in time, without interrupting your key send:

def slowly_send_keys(field, text):
    for c in text:
        field.send_keys(c)
        time.sleep(0.1)

You can call this as such:

input = form_element.find_element_by_xpath(password_xpath)
slowly_send_keys(input, "password")

We are just waiting 0.1s between each key press here. Sometimes Selenium sends keys very quickly, and not all keystrokes get registered, so this solution is meant to work around that potential issue.

Mentioned in Debanjan's solution above, invoking WebDriverWait on the password field should also help resolve your issue -- I wanted to provide an alternative to send_keys in case that was part of the issue here as well.

You Can Send Keys By Adding Wait There Because Some Time Element Not Clickable And Also Make Sure That Your Are Giving Right Xpath !

If You Don't Give Right XPATH It Will Also Give You Error

For Correct Xpath you can use selector gadget in chrome

Well If You Are Give Right XPATH Then:

Try This Code!

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

input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "YOUR XPATH"))).send_keys("Your Password")

You Can Also Add Wait After Character Here Is The Way!

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

input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "YOUR XPATH")))

for p in input:
    input.send_keys(p)
    time.sleep(0.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM