Python selenium send_keys emoji support

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

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:

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) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!