I'm trying to send a message using send_keys()
by picking a random item containing an emoji from a list:
gm = [
'Good morning 🌞',
'Good morning ☀️',
'Good morning ⛅',
'Good morning 🌅',
'Good morning 🌄',
'Good morning 🌻',
'Good morning ⛅',
'Good morning 🌥️',
'Good morning 🌤️',
'Good morning ☕',
]
Sending the text directly containing an emoji works fine like so:
textbox = driver.find_element(By.XPATH, textbox_xpath)
textbox.send_keys('Good morning ☀️' + Keys().ENTER)
But having it pick a random value from the list as the text input does not work and results in the error: selenium.common.exceptions.WebDriverException: Message: unknown error: ChromeDriver only supports characters in the BMP
.
textbox = driver.find_element(By.XPATH, textbox_xpath)
textbox.send_keys(random.choice(gm) + Keys().ENTER)
Any help would be appreciated.
Turns out, I was never able to directly send most of the emojis besides this one: ☀️, because of some Unicode issues. Encoding with ('utf-8')
also did not work. Instead, I created a workaround to simply use the Clipboard by copying a random emoji directly from https://emojipedia.org/ in a new tab and then sending a Ctrl-v
command through sendKeys()
.
EMOJI_XPATH = '/html/body/div[5]/div[1]/article/section[1]/form/label/button'
emoji_list = [
'https://emojipedia.org/sun/',
'https://emojipedia.org/sun-with-face/',
'https://emojipedia.org/sunrise/',
]
driver.get(random.choice(emoji_list))
emoji = driver.find_element(By.XPATH, EMOJI_XPATH)
emoji.click() # clicks 'Copy' button
# runs JS code to open a new tab
driver.execute_script("window.open('about:blank', 'secondtab');")
driver.switch_to.window('secondtab')
driver.get('https://yourURL.com')
textbox = driver.find_element(By.XPATH, TEXTBOX_XPATH)
textbox.send_keys('Good morning ' + Keys().CONTROL, 'v')
textbox.send_keys(Keys().ENTER)