简体   繁体   English

Chromedriver + Selenium 无法发送“s”键

[英]Chromedriver + Selenium cannot send 's' key

Using Python 3.7.4, Selenium 3.141.0, Chromedriver 78 and Chrome 78.使用 Python 3.7.4、Selenium 3.141.0、Chromedriver 78 和 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.我正在尝试自动登录网页,但是当我将键发送到表单中的输入时,它会发送除“s”字母之外的所有字符。

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

It only writes "paword".它只写“paword”。 I've seen this issue in other querstions but the Chromedriver version was old.我在其他问题中看到过这个问题,但 Chromedriver 版本很旧。 Also I tried with "\s" but it writes "\".我也试过用“\s”,但它写的是“\”。

Didn't find any documentation of this.没有找到这方面的任何文件。 I tried with Chrome 77 + Chromedriver 77, same result.我尝试使用 Chrome 77 + Chromedriver 77,结果相同。 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.当您尝试识别密码字段并随后发送字符序列时,可能正在进行某些JavaScriptAJAX调用。 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:要定位并向元素发送字符序列,您必须为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一解决方案:

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "password_css"))).send_keys("password")
  • Using XPATH :使用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.您可以将send_keys包装在一种方法中,以便在每次按下字符之间等待一段时间来缓慢发送键。 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:这可能有助于减慢键发送速度并允许该字段上的任何 Javascript 事件及时触发,而不会中断您的键发送:

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.我们只是在每次按键之间等待 0.1 秒。 Sometimes Selenium sends keys very quickly, and not all keystrokes get registered, so this solution is meant to work around that potential issue.有时 Selenium 发送键的速度非常快,并且并非所有击键都会被注册,因此此解决方案旨在解决该潜在问题。

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.在上面的 Debanjan 解决方案中提到,在密码字段上调用WebDriverWait也应该有助于解决您的问题——我想提供send_keys的替代方案,以防这也是这里问题的一部分。

You Can Send Keys By Adding Wait There Because Some Time Element Not Clickable And Also Make Sure That Your Are Giving Right Xpath !您可以通过在此处添加等待来Send Keys ,因为某些时间Element Not Clickable ,还要确保您提供正确的Xpath

If You Don't Give Right XPATH It Will Also Give You Error如果您不提供正确XPATH ,它也会给您错误

For Correct Xpath you can use selector gadget in chrome对于正确的Xpath ,您可以在 chrome 中使用选择器小工具

Well If You Are Give Right XPATH Then:好吧,如果您提供正确的XPATH ,那么:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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