Using Selenium 2.0 with Python 3.2

Posted on Wednesday, May 4th, 2011 at 4:48 pm

Currently the Python Bindings for Selenium are not completely compatible with Python 3.2. I’ll show here how to make the python files compatible with version 3.2 using the python 2to3 utility. I am assuming here that you have Python installed and running, you have downloaded and installed Selenium 2.0, and that you have downloaded the Python Bindings and dropped the ‘selenium’ directory into your python ‘Lib’ directory to be used as a module.

So if you have all of this installed and you run the following line in the Python IDLE

from selenium import webdriver

you will most likely (considering that the Binding code has not been updated between now and then) get an error indicating a syntax error on line 193 or thereabouts. This syntax error is occurring because the Binding code is written for python version 2.7.

Good news though. Python 3.2 comes with a utility in the ‘Tools’ directory that will convert any .py file or files from version 2 to version 3.

If you want to find the actual file you can find it in the Python32/Tools/Scripts directory. It is named 2to3.py.

So we’ll execute this utility from the command line. So if on windows do Start/Run – type cmd to open the prompt. On mac use Applications/Utilities/Terminal.


Now open the Python32/Lib directory and copy the selenium directory you dropped in here when you downloaded the Selenium Python binding. For safekeeping paste it to the desktop or somewhere as a backup. Now paste it into the Python32/Tools/Scripts directory with the 2to3.py utility.

Back in the command prompt change into the Tools directory (something like ‘cd /Python32/Tools/Scripts). Now execute the 2to3 utility by typing ’2to3.py selenium’ without the quotes in the command prompt. This essentially runs the utility and passes the directory you wish to convert (in this case the ‘selenium’ directory). If you wish you can also pass a single .py file as well. After executing you will notice that the console outputs a diff of the original and what would be changed to conform to Version 3.2. Note though that no changes were actually made. If you are happy with the updates now run ’2to3.py -w selenium’ in the command prompt without the single quotes. This will actually execute the script. Notice now in the selenium directory in the Scripts directory you now have a bunch of .bak files. These are the original copies of all files converted to version 3. Pretty cool.

Once you have the updated files now copy the Scripts/Selenium directory back into the Lib directory overwriting the original. Now when you run

from selenium import webdriver

you should not get the syntax error. Check that it was imported correctly by running

dir(webdrover)

You should be able to see the contents of the module now.

CategoryPython
One Response to “Using Selenium 2.0 with Python 3.2”
  1. [...] pukkared Web Design & Development Using Selenium 2.0 with Python 3.2 [...]

Leave a Reply

*
(Won't be published) *