Python selenium send_keys emoji support

Viewed 4997

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.

1 Answers
Related