【问题标题】:python selenium send_keys emoticons characterspython selenium send_keys 表情符号字符
【发布时间】:2016-01-08 15:14:02
【问题描述】:

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

����������✊?????????????????????????????????����

硒返回错误,我用.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

表情示例:

���������✊????�????�????�????�????�����

【问题讨论】:

  • 你应该试试encode('utf-8')。如果没有mcve,我们将无能为力...
  • 是的,我尝试并给出了同样的错误
  • 错误是什么?
  • 'ascii' 编解码器无法解码位置 48 的字节 0xef:序数不在范围内 (128)
  • 另外,你的字符编码似乎很糟糕......你可以试试'????????????????????'.encode('utf-8') 吗?

标签: python selenium selenium-webdriver


【解决方案1】:

这就是我所做的。

# -*- 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
    ))

【讨论】:

    【解决方案2】:

    我是用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2021-10-17
      • 2017-05-27
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多