Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 628
abu Avatar asked Aug 06 '18 11:08

abu


People also ask

How do you send Emojis using selenium?

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.

How do you send Emojis in Python?

Using emoji module: Emojis can also be implemented by using the emoji module provided in Python. To install it run the following in the terminal. emojize() function requires the CLDR short name to be passed in it as the parameter. It then returns the corresponding emoji.


4 Answers

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)
like image 133
mch22 Avatar answered Nov 16 '22 06:11

mch22


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)
like image 29
Mahmudur Rahman Shovon Avatar answered Nov 16 '22 05:11

Mahmudur Rahman Shovon


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

like image 38
Anchal Agrawal Avatar answered Nov 16 '22 07:11

Anchal Agrawal


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

like image 23
Ricardo Diaz Avatar answered Nov 16 '22 07:11

Ricardo Diaz