V
V
Vladislav Polyakov2017-04-05 22:21:35
Python
Vladislav Polyakov, 2017-04-05 22:21:35

How to change the encoding of received data by selenium WebDriver?

I've been betting for a week now, I can't solve this problem: With the help of Selenium Web Driver, I get data from the site, but there are Russian characters there, and when parsing, it gives an error

UnicodeEncodeError: 'charmap' codec can't encode character

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import ...
os.system('chcp 65001')
...
        for i in self.driver.find_elements(By.XPATH, '//*[@id="modal-content"]'):
            print(i.get_attribute('innerHTML'))
...

And where �������� displays Cyrillic
AND throws an error UnicodeEncodeError: 'charmap' codec can't encode character '\xd8' in position 571: character maps to . Help solve the problem, the default console encoding is set to chcp 65001.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sigure, 2017-04-06
@Zelxat

try:
somth.encode(encoding="UTF-8")

V
Vladislav Polyakov, 2017-04-06
@polRk

Here is the solution to my problem.

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question