Andrew McDonald
Andrew McDonald

Reputation: 21

Getting TypeError: "POST data should be bytes or an iterable of bytes. It cannot be str." while following simply online example

I'm attempting to learn some automated testing, but have hit a wall right at the start.

I'm getting the error in the title when I try to run the very simple example from this web site: https://gist.github.com/1126917

Here's the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()

I've tried debugging but I didn't have any luck with that. I can't even tell what piece of data is typed wrong. Keep in mind that I just installed most of this into my environment, so I haven't been messing around much.

Relevant info:

Thanks for any help.

Upvotes: 2

Views: 1115

Answers (2)

Lev Levitsky
Lev Levitsky

Reputation: 65811

The online example is in Python 2, where str and bytes are essentially the same thing.

Have a look at this or better this for explanation.

Briefly, in Python 3 you need explicit conversion between str (which is a Unicode string) and bytes (which is an encoded string). That's one of the major differences between Python 2.x and 3.x.

Taking Tim's answer into account, the error is most likely to happen somewhere inside selenium module, so you should probably install Python 2.7 to work with Selenium.

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336378

As stated in the docs, Selenium bindings for Python are only compatible with Python 2.6 and 2.7.

Upvotes: 1

Related Questions