Chromedriver only supports characters in the BMP error while sending Emoji with ChromeDriver Chrome using Selenium Python to Tkinter's label() textbox

后端 未结 3 1568
忘掉有多难
忘掉有多难 2020-11-30 13:01

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

相关标签:
3条回答
  • 2020-11-30 13:19

    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("                                                                    
    0 讨论(0)
  • 2020-11-30 13:20

    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 = "                                                                    
    0 讨论(0)
  • 2020-11-30 13:33

    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, '                                                                    
    0 讨论(0)
提交回复
热议问题