im trying to send keys with spanish accents in selenium, what im doing is sending strings via a array with various entrys, here is the line where it get stucks.
["Electrodomésticos", "Otros electrodomésticos", ["sensorhumo.jpg"], "Sensor de humo inalámbrico independiente.", "-Frecuencia: 433Mhz. -Codigo de trabajo: 2262. -Alacance inalámbrico: 80 mts con línea de vista. ", "59000", "x", "x", "x", "x", "x", "x"],
when i send this part:
"-Frecuencia: 433Mhz. -Codigo de trabajo: 2262. -Alacance inalámbrico: 80 mts con línea de vista. "
to this code:
descripcion=".//*[@id='field-description']"
descripciontext=str(array3[i][x])
x=x+1
descripcionelement = wait.until(lambda driver: driver.find_element_by_xpath(descripcion))
descripcionelement.send_keys(descripciontext)
it throws this message:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0: unexpected end of data
it only works if i remove the accents from this part and i tried using decode.
I've stumbled with this error myself, when I was trying to send something like 'España' in a select option inside a form.
The accepted answer is right, the best approach is to send values in unicode.
I'll leave here a short easy code, that transforms the value that is sent, either str or unicode, in a transparent way
def _convert(param):
if isinstance(param, str):
return param.decode('utf-8')
else:
return param
# both examples will work
parameter = 'España'
driver_element.send_keys(_convert(parameter))
unicode_parameter = u'España'
driver_element.send_keys(_convert(parameter))
To handle unicode in selenium-sendkeys, unicode casting is needed, to convert bytes into unicode use one of the ways-
descripciontext = "-Frecuencia: 433Mhz. -Codigo de trabajo: 2262. -Alacance inalámbrico: 80 mts con línea de vista."
Unicode (by manual): descripciontext = u"-Frecuencia: 433Mhz. -Codigo de trabajo: 2262. -Alacance inalámbrico: 80 mts con línea de vista."
Or
Unicode (by automatic decoding): descripciontext = unicode(descripciontext.decode("iso-8859-4"))
Now use send_keys
descripcionelement.send_keys(descripciontext)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With