Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python selenium send_keys emoji support

I am trying to use selenium's send_keys to send emoji characters to a text box with the following python code.

browser = webdriver.Chrome(chrome_options=options)
browser.get("https://www.google.co.in")
time.sleep(5)
working = browser.find_element_by_id('lst-ib')
text = u'Python is πŸ‘'
working.send_keys(text)

I am getting the following error.

Traceback (most recent call last):
  File "smiley.py", line 30, in <module>
    working.send_keys(text)
  File "C:\Python27\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 "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 491, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: missing command parameters

Upon searching I found out that selenium send_keys only support single byte characters. But I would like to know, if there is any alternative way I could achieve this.

I have tried using CTRl + C and CTRL + V combinations and it is working. But sometimes the clipboard contents gets changed before pasting (copy and paste happens almost immediately).

I could not set the value of the field using execute_script either, as it is not an input element but a span. I could set inner_text but useless in my use case.

UPDATE: I have used some workarounds to tackle the clipboard overwritten issue. The working code is provided below.

schedule = urllib2.urlopen(request,context=ctx)
text = schedule.read().decode('utf-8')
elem = browser.find_element_by_class_name(xpathObj['commentLink'])
elem.click()
time.sleep(2)
ActionChains(browser).send_keys('a').perform()
time.sleep(2)
copy_attempt = 0
while True:
    span_element = browser.find_element_by_xpath(xpathObj['commentSpan'])
    browser.execute_script("arguments[0].innerText = arguments[1]", span_element, text)
    time.sleep(2)
    ActionChains(browser).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
    time.sleep(1)
    ActionChains(browser).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).key_down(Keys.CONTROL).send_keys('V').key_up(Keys.CONTROL).perform()
    time.sleep(1)
    win32clipboard.OpenClipboard()
    text1 = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
    emoji_pattern = re.compile(
        u"(\ud83d[\ude00-\ude4f])|"  # emoticons
        u"(\ud83c[\udf00-\uffff])|"  # symbols & pictographs (1 of 2)
        u"(\ud83d[\u0000-\uddff])|"  # symbols & pictographs (2 of 2)
        u"(\ud83d[\ude80-\udeff])|"  # transport & map symbols
        u"(\ud83c[\udde0-\uddff])"   # flags (iOS)
        "+", flags=re.UNICODE)
    s = difflib.SequenceMatcher(None,emoji_pattern.sub(r'', text), emoji_pattern.sub(r'', text1))
    if (s.ratio() < 1.0):
        if (copy_attempt < 5):
            copy_attempt = copy_attempt + 1
            continue
        else:
            break
    else:
        ActionChains(browser).send_keys(Keys.RETURN).perform()
        time.sleep(3)
        break
time.sleep(3)

Thank you All.

like image 562
Martin Solomon Avatar asked Jul 26 '17 14:07

Martin Solomon


People also ask

How do you send Emojis in selenium?

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. Show activity on this post. Show activity on this post. Copy the emoji to clipboard and paste in the input element will work too.

How do you send Emojis in Python?

Using Unicodes: Every emoji has a Unicode associated with it. Emojis also have a CLDR short name, which can also be used. From the list of unicodes, replace β€œ+” with β€œ000”. For example – β€œU+1F600” will become β€œU0001F600” and prefix the unicode with β€œ\” and print it.

Does selenium work with Python?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc.


1 Answers

With the current version of the chrome driver (2.32), the method send_keys only supports characters from the Basic Multilingual Plane.

If you wish to type an emoji, you'll have to use a script injection to write your text in the desired input via the value property. You'll also have to dispatch the change event to notify the listeners :

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.co.in")

time.sleep(5)

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('lst-ib')
text = u'\ud83d\udc4d'

driver.execute_script(JS_ADD_TEXT_TO_INPUT, elem, text)
like image 70
Florent B. Avatar answered Sep 22 '22 05:09

Florent B.