Selenium send_keys 不发送撇号
Selenium send_keys doesn't sent apostrophe
在Selenium中我想输入一个测试字符串"hello'world"
,但是网页的文本框变成了"helloworld"
。好像撇号不存在一样。将 "'"
替换为 chr(39)
或拆分字符串也不起作用。
- 我的部分代码:(在python中使用Chrome webdriver)
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)
- 浏览器截图:
在Google主页的搜索框中发送字符序列 hello'world 您需要为 element_to_be_clickable()
引入 WebDriverWait 并且您可以使用以下 :
使用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()
时 非美国键盘设置 和 Unicode 字符 存在一些问题。您可以在以下位置找到一些相关讨论:
- SendKeys does not work correctly with non-US keyboard settings
- send_keys("é") to IE11 element sends e instead of é
- 3.5.0 Grid - Selenium Keys / Unicode Failing
此问题已通过提交 Fixing encoding of payload passed by hub to a node.
解决
使用 Selenium v3.5.3 应该可以解决这个问题。
tl;博士
解法:
使用 geckodriver 从 Chromedriver 更改为 Firefox。单引号和双引号在当前版本的 Chromedriver 中似乎有问题。
在Selenium中我想输入一个测试字符串"hello'world"
,但是网页的文本框变成了"helloworld"
。好像撇号不存在一样。将 "'"
替换为 chr(39)
或拆分字符串也不起作用。
- 我的部分代码:(在python中使用Chrome webdriver)
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)
- 浏览器截图:
在Google主页的搜索框中发送字符序列 hello'world 您需要为 element_to_be_clickable()
引入 WebDriverWait 并且您可以使用以下
使用
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()
时 非美国键盘设置 和 Unicode 字符 存在一些问题。您可以在以下位置找到一些相关讨论:
- SendKeys does not work correctly with non-US keyboard settings
- send_keys("é") to IE11 element sends e instead of é
- 3.5.0 Grid - Selenium Keys / Unicode Failing
此问题已通过提交 Fixing encoding of payload passed by hub to a node.
解决使用 Selenium v3.5.3 应该可以解决这个问题。
tl;博士
解法: 使用 geckodriver 从 Chromedriver 更改为 Firefox。单引号和双引号在当前版本的 Chromedriver 中似乎有问题。