「Selemium」- ChromeDriver only supports characters in the BMP @20210211

问题描述

在 Selenium 中,执行自动化测试任务,产生如下错误:

Caught: org.openqa.selenium.WebDriverException: unknown error: ChromeDriver only supports characters in the BMP
  (Session info: chrome=87.0.4280.66)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'laptop-k53sd', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-47-generic', java.version: '1.8.0_265'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.66, chrome: {chromedriverVersion: 87.0.4280.66 (fd98a29dd59b3..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:42727}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true, webdriver.remote.sessionid: 7569ceb732eda9f22ba68994b0e...}
Session ID: 7569ceb732eda9f22ba68994b0ed1ee4
org.openqa.selenium.WebDriverException: unknown error: ChromeDriver only supports characters in the BMP
  (Session info: chrome=87.0.4280.66)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'laptop-k53sd', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-47-generic', java.version: '1.8.0_265'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.66, chrome: {chromedriverVersion: 87.0.4280.66 (fd98a29dd59b3..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:42727}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true, webdriver.remote.sessionid: 7569ceb732eda9f22ba68994b0e...}
Session ID: 7569ceb732eda9f22ba68994b0ed1ee4
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
	at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
	at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
	at OschinaSeleniumCommand.actionPublishPost(toOschinaUsingSelenium.groovy:208)
	at OschinaSeleniumCommand$actionPublishPost$0.call(Unknown Source)
	at OschinaSeleniumCommand.main(toOschinaUsingSelenium.groovy:72)

问题原因

针对我们的问题:在页面中,包含 Emoji 表情,这些表情是无法通过 sendKeys() 方法发送的。

解决方案

方法一、更换驱动,使用 GeckoDriver / Firefox 可以解决问题。(我们没有尝试该方法,但是回答者说可以解决问题,我们这里仅作记录)

方法二、不再使用 sendKeys 方法,而是执行 JavaScript 代码:

from selenium import webdriver

// 将要执行的代码
JS_ADD_TEXT_TO_INPUT = """
  var elm = arguments[0], txt = arguments[1];
  elm.value += txt;
  elm.dispatchEvent(new Event('change'));
"""

// 将要输入文本的元素(带有 Emoji 字符)
browser = webdriver.Chrome('C:\\Python37\\chromedriver.exe')
browser.get("https://google.com/")
elem = browser.find_element_by_name('q')

// 将要输入的文本
text = "
上一篇:Omission is the only source of error in the secon


下一篇:UnexpectedRollbackException解决方案