10

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?

abu
  • 737
  • 5
  • 8
  • 19
  • 3
    I have no idea if that would work for the whatsapp web page, but just wondering if `message.send_keys("♥")` would work.... – Ofer Sadan Aug 06 '18 at 11:12
  • 1
    Nice, this works! However, I would be more satisfied with a solution that would work for different emojis as well. Bcs not everyone can be inputted as this. – abu Aug 06 '18 at 11:18
  • 1
    I'm not entirely sure that whatsapp has all of its emojis defined as ascii symbols, so it probably wouldn't be possible for all of them. After all, even when you do that manually you don't "send a key" with a poop emoji... you select it from a list of pictures. That being said I already found several more keys that work like that: ☺♥♦♣♠ for example – Ofer Sadan Aug 06 '18 at 11:25

6 Answers6

10

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

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)
mch22
  • 101
  • 1
  • 2
5

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

Anchal Agrawal
  • 364
  • 2
  • 7
  • The "multiple unicodes" comment is vaguely misleading; this is an example of a UTF-16 surrogate sequence (which is how you represent code points above 0xFFFF in this notation). – tripleee May 13 '19 at 07:15
  • in case someone needs a website with the complex unicodes: https://www.nowsms.com/emoticons.htm – shiny Jan 31 '21 at 17:24
5

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

Ricardo Diaz
  • 51
  • 1
  • 2
0

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

0

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!