Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium webdriver and unicode

It's my 2nd day with Selenium 2 library and the pain with Unicode never seem to subside.

I'm just doing the most basic operation, want to print the page source:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("http://google.com")

print driver.page_source

Sure enough, I get an error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0119' in position 62045:  
ordinal not in range(128)

How can I please encode this to utf-8 ?

like image 754
nutship Avatar asked May 29 '13 20:05

nutship


2 Answers

You have options, based on this similar question.

You can either convert the source to all ascii losing the Unicode characters in the process.

(driver.page_source).encode('ascii', 'ignore')

Or, and I think you'll prefer this, you can encode it to utf-8 like this: (driver.page_source).encode('utf-8').

like image 75
jaynp Avatar answered Nov 11 '22 07:11

jaynp


Instead of print(string), use print(repr(string)) to return a printable representation of the object.

like image 26
ron_g Avatar answered Nov 11 '22 05:11

ron_g