0

I'm trying to get a text from Google Sheets, which is encoded in UTF-8, and copy it into any text field web element, but selenium throws an error when I try to send_keys to the text field.

I've tried not encoding the UTF-8 text and encoding it and in both situations, I get an error.

post description = 'Science! ✨' # (just an example)
post_description = post_description.encode('utf-8')

driver.get("https://www.editpad.org/")
time.sleep(10)

inputElement = driver.find_element_by_css_selector("#text")
inputElement.send_keys(post_description)

Here's what I get with the encoding:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xf0 in position 0: unexpected end of data

Here's what I get without the encoding:

selenium.common.exceptions.WebDriverException: Message: missing command parameters

I've researched this a lot and can't seem to find an obvious solution to copy-pasting utf-8 text from a source to another.

Any ideas?

raresm
  • 93
  • 1
  • 11
  • Works well. No `UnicodeDecodeError` – Nouman Aug 25 '19 at 09:33
  • @BlackThunder on what system are you running it? – raresm Aug 25 '19 at 09:38
  • 1
    I am using Windows 10, with python 3.7.3 – Nouman Aug 25 '19 at 09:38
  • Just for making sure, I am testing with selenium – Nouman Aug 25 '19 at 09:40
  • @BlackThunder there are many other strings like this one with many other chars. Do you have any idea why some work and others don't? – raresm Aug 25 '19 at 09:41
  • Encoding works perfectly for me but looks like selenium can't handle such chars. `TypeError: sequence item 0: expected str instance, int found` – Nouman Aug 25 '19 at 09:43
  • After encoding, this is what I get: `Science! \xf0\x9f\x94\xad\xf0\x9f\x92\xab\xf0\x9f\x9a\x80\xe2\x9c\xa8` – Nouman Aug 25 '19 at 09:45
  • I guess the only way then is to copy it to clipboard and paste it by sending CTRL+V? I've tried that with Tkinter as it has clipboard support but I think there's an issue with that method as well because it crashes the browser when I try to paste it. I'll try again and see where exactly it breaks down. I just wanted to make sure it's not my setup or a python or selenium problem. – raresm Aug 25 '19 at 09:52
  • @BlackThunder it may work for that exact text but for longer strings with more chars it may not. It's really important for the program to have a reliable way to copy-paste all of the text as I could probably remove those missing bytes otherwise with unicodedata.normalize. – raresm Aug 25 '19 at 09:55

0 Answers0