Python selenium send_keys emoji support

I am trying to use selenium send_keys to send emoji characters to a textbox 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

      

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

I tried using combinations of CTRl + C and CTRL + V and it works. But sometimes the contents of the clipboard change before pasting (copying and pasting happens almost immediately).

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

UPDATE: I have used some workarounds to solve the clipboard overwriting issue. The working code is shown 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)

      

Thanks everyone.

+3


source to share


1 answer


In the current version of the chrome driver (2.32), the method send_keys

only supports characters from the Basic Multilingual Plane .

If you want to inject emoji, you will need to use script injection to write the text in the desired input using a property value

. You also need to dispatch an event change

to notify 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)

      

+2


source







All Articles