首页 > 解决方案 > Selenium send_keys 不发送撇号

问题描述

在 Selenium 中,我想输入一个 teststring "hello'world",但网页的文本框变为"helloworld". 好像撇号不存在一样。替换或拆分字符串也不起作用"'"chr(39)

driver = webdriver.Chrome()
driver.get("https://google.com")
text = "hello'world"
textbox = driver.find_element_by_xpath('//* 
[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input')
for i in text:
    textbox.send_keys(i)
    sleep(0.1)

标签: pythonseleniumselenium-webdriverwebdriverwaitsendkeys

解决方案


要在Google Home Page的搜索框中发送字符序列 hello'world ,您需要诱导WebDriverWait for the ,您可以使用以下 Locator Strategyelement_to_be_clickable()

  • 使用CSS_SELECTOR

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://google.com")
    text = "hello'world"
    textbox = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
    for i in text:
        textbox.send_keys(i)
    
  • 浏览器快照:

撇号


更新

以前似乎在调用. _ 您可以在以下位置找到一些相关的讨论:send_keys()

此问题已通过 hub 传递到 node 的有效负载的提交修复编码得到解决。

使用Selenium v​​3.5.3应该可以解决这个问题。


tl; 博士

更改键盘布局


推荐阅读