5

I'm using Selenium Webdriver with python for running tests on a website.

python 2.7.2 with latest Selenium

I can't figure out how to send unicode such as German "Umlaute" (öäüß) to an input form. As far as I know webdriver can handle unicode so this might be a python problem.

# -*- coding: iso-8859-1 -*-

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.google.de")
i = u"hälp me"
driver.find_element_by_css_selector("INPUT[name=\"q\"]").send_keys(i)

This works without errors but does not send the "ä". Is there any further encoding/decoding required...?

Cheers

6
  • Presumably the HTML page has an encoding; encode the input to that encoding before calling .send_keys() might work? google.de sez it's encoded to ISO-8859-1, so try .send_keys(i.encode('latin1'). Commented Jan 10, 2013 at 10:17
  • what does it send instead of the ä sign? Commented Jan 10, 2013 at 10:22
  • it sends nothing instead of the ä. If google.de runs on ISO-8859-1 I should be good with i="hälp me" since my file is encoded # -*- coding: iso-8859-1 -*- but that throws an error UnicodeDecodeError: 'utf8' codec can't decode byte 0x8a in position 0: invalid start byte. i.encode("latin1") doesn't work either - same error Commented Jan 10, 2013 at 10:37
  • I've also tried yahoo.de with is utf-8 encoded with the original code... no success. Also I've tried the Chrome webdriver but it gives the same error. Moreover, I've tried i.decode("iso-8859-1").encode("utf-8") with and without the u in the variable declaration. No success, I'm running out of ideas =( Commented Jan 10, 2013 at 12:55
  • something like this: i = unicode(i, "utf-8") maybe? Commented Jan 10, 2013 at 13:46

1 Answer 1

5

After some hours of trying I finally got it =) Looks like my IDE (Komodo) caused the problem - after setting the encoding in preferences to latin-4 it works nicely in two different ways:

Declare string as unicode:

# -*- coding: iso-8859-4 -*-

from selenium import webdriver
from sys import version_info

driver = webdriver.Firefox()

driver.get("http://www.google.de")

i = u"hälp me"
driver.find_element_by_css_selector("INPUT[name=\"q\"]").send_keys(i)

Decode latin-4 and convert to unicode:

# -*- coding: iso-8859-4 -*-

from selenium import webdriver
from sys import version_info

driver = webdriver.Firefox()

driver.get("http://www.google.de")

i = "hälp me"
i = unicode(i.decode("iso-8859-4"))
driver.find_element_by_css_selector("INPUT[name=\"q\"]").send_keys(i)

Thanks to everybody who helped me!

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

(1) don't use non-ascii characters inside bytestrings (it is explicitly forbidden in Python 3). Either use u'' unicode literals or add from __future__ import unicode_literals at the top, to create Unicode string. (2) unrelated: use single quotes, to avoid escaping double quotes in Python source: '[name="q"]'. (3) use utf-8 encoding for your Python source (configure your IDE/editors accordingly) unless you have a very good reason not to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.