【问题标题】:Chromedriver only supports characters in the BMP error while sending Emoji with ChromeDriver Chrome using Selenium Python to Tkinter's label() textboxChromedriver only supports characters in the BMP error while sending Emoji with ChromeDriver Chrome using Selenium Python to Tkinter\'s label() textbox
【发布时间】:2022-08-18 21:06:08
【问题描述】:

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?

【问题讨论】:

  • Which emoji did you try?

标签: python selenium google-chrome selenium-chromedriver emoji


【解决方案1】:

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)

【讨论】:

  • 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, are you try send just text? With text it works ok? And what version of webdriver you use?
  • This is not working anymore, What is new Event('change') ? Is there a possibility that it has been changed ?
【解决方案2】:

This error message...

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

...implies that theChromeDriverwas unable to send theemojisignal through send_keys() method.

ChromeDriver only supports characters in the BMP is a known issue withChromiumteam asChromeDriverstill 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 sendSMPcharacters (e.g.CJK,Emojis,Symbols, etc) raises the error.


Alternative

A potential alternative would be to useGeckoDriver / 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:UtilityBrowserDriversgeckodriver.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:

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:

【讨论】:

  • This solved my problem, thank you so much! Simply changing from chromedriver -> geckodriver and google chrome -> firefox allowed me to send emojis through. Cheers!
  • ok so using a different driver and browser is an answer to chrome driver problem of supporting chars only in basic multilingual plane?
【解决方案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)

【讨论】:

  • 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
  • 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"
  • Can you share the code on some platform and paste the link here?
  • remove the async and await part - those are js syntax
  • 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
【解决方案4】:

there was an open bug on chrome driver website, the bug as been fixed this month (14.11.21)

https://bugs.chromium.org/p/chromedriver/issues/detail?id=2269

this is the bug report.

how to use new chrome driver:

first go to: https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Win/942085/

download the chrome driver from there.(the first mark as showen in image).

then you will need to install the new chrome v98 using the mini_installer.exe

(pay attantion its all expermintal),

without this you could not use the new driver due to version compitity.

next i needed to delete the current chrome version to make this work good.

your are all welcome to ask me question about it, i mange to make it work with the versions i add here on whatsapp using selenium chrome driver.

EDIT: chrome versions 98 stable will come out in 30 days, so you could try to install the beta version and not the chromium version and see if it's working.

you don't have to delete the old chrome version, I know this thread is about python but in java (i guess python support it too) you could do this:

options.setBinary(System.getProperty("user.home") + "\AppData\Local\Chromium\Application\chrome.exe");

and it will set the path to the chromium version.

【讨论】:

    【解决方案5】:

    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.

    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()
    

    【讨论】:

      【解决方案6】:

      Here is how I fixed this using VBA and ChromeDriver in Excel SeleniumBasic:

      objIE.ExecuteScript "arguments[0].value = arguments[1]", Array(objIE.FindElementById("sqlvalue1"), Sheets("SheetName").Range("A1").Value)
      

      This avoids using SendKeys which is what errors when it is run with this error message.

      【讨论】:

        【解决方案7】:

        You can use

        js_code = """
          var elm = arguments[0], txt = arguments[1];
          elm.value += txt;
          elm.dispatchEvent(new Event('change'));
        """
        
        your_text = 'this my text ????'
        element = driver.find_element_by_xpath('//*[@data-testid="input"]')
        driver.execute_script(js_code, element, your_text)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-06-02
          • 1970-01-01
          • 1970-01-01
          • 2022-02-20
          • 2020-03-27
          • 1970-01-01
          • 2021-11-23
          • 2020-05-27
          相关资源
          最近更新 更多