1

I was trying to send a emoji with python and the Selenium Chrome Driver when the following error accured:

selenium.common.exceptions.WebDriverException: Message: unknown error: ChromeDriver only supports characters in the BMP


(Session info: chrome=86.0.4240.75)

I already researched this topic for a while and found the following workaround in C# in another Question from @GalBracha.

Since I don't know C# at all and I am not too familiar with Selenium I wanted to ask if someone knows a similar solution in Python.

Thanks

Ph.lpp
  • 484
  • 1
  • 4
  • 17

2 Answers2

6

Copy & Paste it

It was so easy to round on this issue by copying the text to the clipboard and then pasting it into the element. pasting a text that contains emojis

import pyperclip
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://google.com")
title = driver.title
assert title == "Google"

driver.implicitly_wait(0.5)

search_box = driver.find_element(by=By.NAME, value="q")

pyperclip.copy("Hi  This is a test message ! ")
act = ActionChains(driver)
act.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
TheEngineer
  • 193
  • 2
  • 6
3

I'm using Google Images as an example here. You can replace emoji with the desired emoji unicode. You can find the codes here

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get('https://images.google.com/')

search = browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input') 
JS_ADD_TEXT_TO_INPUT = """
  var elm = arguments[0], txt = arguments[1];
  elm.value += txt;
  elm.dispatchEvent(new Event('change'));
  """

emoji = u'\U0001F600'

browser.execute_script(JS_ADD_TEXT_TO_INPUT, search, emoji)

The unicode will look lile U+1F603. All you need to do is replace the '+' with '000' with a \ before the 'U' too look like \U000F603

prxvidxnce
  • 369
  • 1
  • 10