selenium in python3.x error

This is the place for queries that don't fit in any of the other categories.

selenium in python3.x error

Postby metulburr » Thu Apr 11, 2013 12:43 am

OK i had this working maybe a month ago. I just cant find the script i molded to work in python3.x to use as a template.

their example code:
Code: Select all
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com".encode('utf-8')) # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:

   browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
   assert 0, "can't find seleniumhq"
browser.close()


Running this under 2.x is fine, but in 3.x you get the Type Error for what looks like encoding problem.

Traceback (most recent call last):
File "forum2.py", line 7, in <module>
browser = webdriver.Firefox() # Get local session of firefox
File "/usr/local/lib/python3.2/dist-packages/selenium-2.31.0-py3.2.egg/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
desired_capabilities=capabilities)
File "/usr/local/lib/python3.2/dist-packages/selenium-2.31.0-py3.2.egg/selenium/webdriver/remote/webdriver.py", line 67, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.2/dist-packages/selenium-2.31.0-py3.2.egg/selenium/webdriver/remote/webdriver.py", line 109, in start_session
'desiredCapabilities': desired_capabilities,
File "/usr/local/lib/python3.2/dist-packages/selenium-2.31.0-py3.2.egg/selenium/webdriver/remote/webdriver.py", line 158, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python3.2/dist-packages/selenium-2.31.0-py3.2.egg/selenium/webdriver/remote/remote_connection.py", line 340, in execute
return self._request(url, method=command_info[0], data=data)
File "/usr/local/lib/python3.2/dist-packages/selenium-2.31.0-py3.2.egg/selenium/webdriver/remote/remote_connection.py", line 387, in _request
response = opener.open(request)
File "/usr/lib/python3.2/urllib/request.py", line 367, in open
req = meth(req)
File "/usr/lib/python3.2/urllib/request.py", line 1065, in do_request_
raise TypeError("POST data should be bytes"
TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.

So the first thing i did is obviously encode the string url. But by doing that it does nothing because the root problem is setting the instance browser, not the method get() with a string?

What sucks is, a month or two ago i molded the example code to work in 3.x, but i must of deleted it, so now i am redoing the same thing i did awhile ago.

I mean i get the same error from just these two line only in a script:
Code: Select all
from selenium import webdriver
browser = webdriver.Firefox()


Is there something in webdriver or Friefox that must be encoded also?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: selenium in python3.x error

Postby micseydel » Thu Apr 11, 2013 6:51 am

This is a big reason I've continued using Python 2 instead of Python 3 D=
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1356
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: selenium in python3.x error

Postby stranac » Thu Apr 11, 2013 8:37 am

Looks like you'll need some modifications to make selenium run with python 3.
This looks helpful:
http://code.google.com/p/selenium/issues/detail?id=2139
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: selenium in python3.x error

Postby metulburr » Thu Apr 11, 2013 10:57 am

yeah i came across that. But i had it working under 3.x a month ago. Wrote a script and had it working fine. I never modified anything last time. Odd. Wow i just read my first post. Over said that, lol.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: micseydel and 2 guests