10

I am automating whatsapp messages and would like to send them out through a tkinter window. In this tkinter window I have created a message box with the help of .label() and I am able to connect to whatsapp web through selenium.

Currently, I am able to send out messages already, but without emojis. When I include emojis, I get this error "Chromedriver only supports characters in the BMP". How can I include emojis?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Malcolm Leck
  • 113
  • 1
  • 6

3 Answers3

8

This error message...

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

...implies that the ChromeDriver was unable to send the emoji signal through send_keys() method.

ChromeDriver only supports characters in the BMP is a known issue with Chromium team as ChromeDriver still doesn't support characters with a Unicode after FFFF. Hence it is impossible to send any character beyond FFFF via ChromeDriver. As a result any attempt to send SMP characters (e.g. CJK, Emojis, Symbols, etc) raises the error.


Alternative

A potential alternative would be to use GeckoDriver / Firefox.

  • Code Block:

      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 = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
      driver.get('https://www.google.com/')
      # Chineese Character
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("")
      # Emoji Character
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("")
    
  • Browser Snapshot:

Emoji

You can find a relevant discussion in OpenQA.Selenium.WebDriverException: 'unknown error: ChromeDriver only supports characters in the BMP while sending an emoji through C# Selenium


Outro

A few links:

Smart Manoj
  • 3,837
  • 2
  • 24
  • 45
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    This solved my problem, thank you so much! Simply changing from chromedriver -> geckodriver and google chrome -> firefox allowed me to send emojis through. Cheers! – Malcolm Leck Dec 03 '19 at 01:31
  • 1
    ok so using a different driver and browser is an answer to chrome driver problem of supporting chars only in basic multilingual plane? – p_champ Mar 08 '20 at 03:33
6

It works for me:

from selenium import webdriver

JS_ADD_TEXT_TO_INPUT = """
  var elm = arguments[0], txt = arguments[1];
  elm.value += txt;
  elm.dispatchEvent(new Event('change'));
  """

browser = webdriver.Chrome('C:\\Python37\\chromedriver.exe')
browser.get("https://google.com/")
elem = browser.find_element_by_name('q')

text = "  " + u'\u2764'

browser.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)

enter image description here

Jackssn
  • 743
  • 7
  • 11
  • I tried this method, in my case `elem` is `browser.find_elements_by_css_selector("div[role='presentation'] div[role='textbox']")` but when I run `browser.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)` nothing happens... could you help? Thanks. (the site I'm on is facebook, in particular I'm trying to enter emoji in the post editor) – sound wave Nov 26 '20 at 12:32
  • sound wave, are you try send just text? With text it works ok? And what version of webdriver you use? – Jackssn Dec 04 '20 at 18:29
3

For those who wants to send emojis on Chrome

Solution

    async sendKeysWithEmojis(element, text) {
        const script = `var elm = arguments[0],
        txt = arguments[1];elm.value += txt;
        elm.dispatchEvent(new Event('keydown', {bubbles: true}));
        elm.dispatchEvent(new Event('keypress', {bubbles: true}));
        elm.dispatchEvent(new Event('input', {bubbles: true}));
        elm.dispatchEvent(new Event('keyup', {bubbles: true}));`;
        await this.driver.executeScript(script, element, text);
    }

Call it like so

const element = await this.driver.findElement(selector);
await sendKeysWithEmojis(element, ' This one shall pass ');

What is happening here? We are emulating native key presses using events

Notice that the {bubbles: true} is optional (Was needed in my case due to a complex wrapped input)

Gal Bracha
  • 14,231
  • 9
  • 61
  • 77
  • do you know if it is possible to use this method in python too? I'm trying to adapt your code but have some problems – sound wave Nov 26 '20 at 19:07
  • 1
    I have no idea but I don't see a reason why not as this code is mostly javascript and one command in python "executeScript" – Gal Bracha Nov 26 '20 at 19:49
  • thanks for fast reply, for example if I run the first code my console prompt this error `SyntaxError: invalid syntax` and it says the error is at `sendKeysWithEmojis` – sound wave Nov 26 '20 at 19:56
  • 1
    Can you share the code on some platform and paste the link here? – Gal Bracha Nov 26 '20 at 20:01
  • The code I run is the one you wrote here in the answer: `async sendKeysWithEmojis(element, text) {...}`, when I run it in python it gives me that error – sound wave Nov 26 '20 at 20:58
  • 1
    remove the `async` and `await` part - those are js syntax – Gal Bracha Nov 26 '20 at 21:01
  • 1
    Haha. yeah `const` is also not part of python language - also the ` (Tag) sign should probably be replaced with """ at the beginning and at the end of the string – Gal Bracha Nov 26 '20 at 21:16
  • thanks, now it says error at `script` ahaha i think there is no way don't worry its ok I don't want to bother you – sound wave Nov 26 '20 at 21:56
  • I suggest you refer to some python course to better know the language. Then you can see an example here of running javascript inside python (Which is what needed to run the above code) - https://stackoverflow.com/questions/5585343/getting-the-return-value-of-javascript-code-in-selenium/5585345#5585345 – Gal Bracha Nov 29 '20 at 11:40