python selenium send_keys表情字符

时间:2016-01-08 15:14:03

标签: python selenium selenium-webdriver

我需要发送带有硒的表情符号,例如:

和selenium返回错误,我使用.send_keys(unicode(bio_text, 'ascii')) # iso-8859-1进行了测试,结果相同。

如何使用python selenium发送这些字符?

python代码:

driver.find_element_by_id("biography").clear()
driver.find_element_by_id("biography").send_keys(unicode('���������✊���������', 'ascii'))  # iso-8859-1

表情符号示例:

���������✊���������

2 个答案:

答案 0 :(得分:0)

这就是我所做的。

# -*- coding: utf-8 -*-
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://google.com')

text = u"''"
text = text.replace("'", "\\'")  # escape single quotes
text = text.encode('utf-8')  # needed to make format function work

driver.execute_script(
    "document.getElementById('lst-ib').value = '{data}'".format(
        data=text
    ))

答案 1 :(得分:0)

我用 xpath 做到了

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
inp_xpath = '//*[@id="main"]/xxxxxxxxx'
input_box = wait.until(EC.presence_of_element_located((By.XPATH, inp_xpath)))
text = '????'
driver.execute_script("arguments[0].innerHTML = '{}'".format(text),input_box)
input_box.send_keys('.')
input_box.send_keys(Keys.BACKSPACE +Keys.ENTER)