How to send emojis using Python Selenium? sendKeys function?

3,608 views
Skip to first unread message

psyche

unread,
Nov 3, 2016, 10:29:59 AM11/3/16
to Selenium Users
# Opening the web browser
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='E:\\jPsych\\particular\\chromedriver.exe')
driver.get("https://www.facebook.com")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_loaded)

# Finding email and password fields and sending the keys
email = driver.find_element_by_id("email")
email.send_keys(username)
pwd = driver.find_element_by_id("pass")
pwd.send_keys(password)
pwd.send_keys(Keys.RETURN)

time.sleep(2)

# now for each post, load facebook page and make the post
for each in prepared:
    print each
    driver.get(excludedurl)
    wait = ui.WebDriverWait(driver, 10)
    wait.until(page_loaded)
    post_box=driver.find_element_by_xpath("//textarea")
    post_box.click()
    post_box.send_keys(each)
    post_it=driver.find_element_by_xpath("//button[@class='_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft']")
    post_it.click()
    time.sleep(3+random.randrange(1,3))


This code is the end of a program I've been working on that makes a series of posts (included in the list "prepared") on a facebook page for me using Selenium web driver stuff.

Everything works fine, but when a post includes an emoji like 😭, I get the following error:


Traceback (most recent call last):
  File "C:\Users\me\Dropbox\coding\tigeradmirersautomatic.py", line 101, in <module>
    post_box.send_keys(each)
  File "E:\Python\lib\site-packages\selenium\webdriver\remote\webelement.py", line 347, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
  File "E:\Python\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "E:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "E:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 163, in check_response
    raise exception_class(value)
WebDriverException: Message: missing command parameters


There seem to be no answers on StackOverflow that address this problem. Can you help me? If my script can't send emojis, its functionality is significantly reduced.

(Please don't tell me to use Facebook Graphs or whatever it's called.)

David

unread,
Nov 4, 2016, 1:07:07 AM11/4/16
to Selenium Users
How are you sending the emoji? Are you clicking on an emoji icon from FB list of emojis? Or are you typing / send keys the emoji? If the latter, what's the value you're actually sending?

psyche

unread,
Nov 4, 2016, 12:51:05 PM11/4/16
to Selenium Users
Text is, for example, "4842 - Late meal quesadilla: you are so amazing and beautiful and every moment I am with you, my life just feels more and more special. I wanna spend every day that I have that free meal swipe with you...forever😍❤️❤️❤️", stored in a python string. I send this string using send_keys. No clicking on emoji icons.

David

unread,
Nov 4, 2016, 3:17:15 PM11/4/16
to Selenium Users
I wonder if it has anything to do with the text format/encoding as it is passed along between python, selenium, and the browser. Do you see the emoji printed out in command prompt/console/terminal if you "print" it out from python?

Some other ideas for workarounds as well as for debugging the issue:
  • use javascript (in selenium) to "set" the value of the textbox with your string instead of native sendKeys, and see if that does the trick or fails somehow as well. For this step, you could also test it outside of selenium 1st to see if it works, simply do in browser developer console: find the element with javascript, once found and stored in say a variable called "elem", do elem.value = "the text with emoji";
  • use a separate tool to send the emoji if selenium doesn't work - e.g. AutoIt, Sikuli
  • try encode/decode the text as necessary? utf-8, base64, etc.
Reply all
Reply to author
Forward
0 new messages