1

Currently I'm trying my first steps in Python by trying to rebuild an Instgram bot.

Unfortunately, every time I run my program I get the error:

SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xe4 in position 23: invalid continuation byte

The codeline from the error is:

like_button = lambda: driver.find_element_by_xpath('//span[@aria-label="Gefällt mir"]').click()

The error is caused by the German umlaut "ä" but I have to use it because it is the xpath from the like button.

I already googled and there was the solution to put # -- coding: utf-8 -- in the first line.

Unfortunately it did not help.

It would be great if you can give me some advice.

Antuan

Elletlar
  • 3,136
  • 7
  • 32
  • 38
Antuan A.
  • 11
  • 1

1 Answers1

-1

The a with umlaut character AFAIK isn't representable in the UTF8 charset. https://en.wikipedia.org/wiki/UTF-8

I would try using ISO-8859-1 as the encoding. https://www.ic.unicamp.br/~stolfi/EXPORT/www/ISO-8859-1-Encoding.html

You can encode the contents of the page at the beginning of your code: driver.find_element_by_tag_name('body').get_attribute('innerHTML').encode("ISO-8859-1")

Or alternatively encode elements as you go in a try/catch to try and isolate special characters.

deefunkt
  • 331
  • 2
  • 12