'Sending emojis with selenium's send_keys()

I would like to send a :heart: emoji with selenium's send_keys()

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome('./chromedriver')

driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)

message = driver.find_elements_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')[0]
message.send_keys(":heart:")

However, this does not work and sends a string ':heart:'.

Could you please suggest how to do it correctly?



Solution 1:[1]

Well,

Here is a workaround! Hope it works!

text_element = driver_trader.find_element_by_xpath('xpath')
text = '???'

driver.execute_script("arguments[0].innerHTML = '{}'".format(text),text_element)
text_element .send_keys('.')
text_element .send_keys(Keys.BACKSPACE)

Solution 2:[2]

guys. I found a really simple but powerfull way to add emoji to whatsApp using Selenium

Message_box.send_keys(':emoji' u'\ue007') # You just have to enter the code cording to the next page

https://gist.github.com/hkan/264423ab0ee720efb55e05a0f5f90887

Solution 3:[3]

Here is a tricky way. we can take help of JavaScript and then driver.execute_script that JS code. You can use this function to do that:

from selenium import webdriver
from selenium.webdriver.common.by import By
import chromedriver_autoinstaller

chromedriver_autoinstaller.install()
driver = webdriver.Chrome()

def paste_content(driver, el, content):
    driver.execute_script(
      f'''
const text = `{content}`;
const dataTransfer = new DataTransfer();
dataTransfer.setData('text', text);
const event = new ClipboardEvent('paste', {{
  clipboardData: dataTransfer,
  bubbles: true
}});
arguments[0].dispatchEvent(event)
''',
      el)

# Go to a page
driver.get('https://some-random-site-as-you-wish.com')
# Find the input box
input_el = driver.find_element(By.XPATH, '//some-xpath')
msg = 'Done! ? ?'
paste_content(driver, input_el, msg)

Solution 4:[4]

You can not do it directly. You need to do this via javascript. Once Javascript has been formatted properly, you need to notify listeners.

Before that, you need to find the unicode of any emoji you want to use. To get codepoint of any emoji, this website can be used: https://emojipedia.org/ Search for any emoji and at the bottom of the page, there will be the codepoint for it.

To convert codepoint to unicode for javascript, use following website: https://r12a.github.io/app-conversion/

Type the codepoint and copy the value in javascript box of it. Once, you have it, use JavaScript as follows:

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

elem = driver.find_element_by_id('<Id of your webelement>')
text = u'\u2764'

driver.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)

2764 is unicode for heart emoji. Hope this helps for any kind of emoticons you want to insert to your whatsapp!

Edit: For more complex emoticons: You need to add multiple unicodes like Emoji for family : ? \uD83D\uDC6A

Solution 5:[5]

Copy the emoji to clipboard and paste in the input element will work too.

Solution 6:[6]

mch22, men your trick is awesome! thank you very much.

just in case someone is wondering how to do a line break inside the text, you can use
labels within the text like this:

text = '?<br>?<br>?'

punctures perfect for my whatsapp bot!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mch22
Solution 2 Ricardo Diaz
Solution 3 Mahmudur Rahman Shovon
Solution 4
Solution 5 SilverStitch
Solution 6 Juan Manuel Oviedo