简体   繁体   中英

Changing Selenium from Py3 to Py2

I installed a Selenium server using Python 3.6. However, I found that the tests I am working on are written in Python2. My shop has asked me to change my Selenium installation so that it can process Python2 code, but I have been unable to successfully make the change.

I have installed Python 2.7.10 and have changed the Windows path to the new Py2. I have tried re-downloading the Selenium server, to no avail. I have also tried pip install selenium in a Py2 environment, and have confirmed the files are in my C:/Python27 folder. Even with the OS pointed at Python 2, Selenium remains pointed at Python 3. How can I change Selenium to run Python2 code instead of Python3?

OS: Win10-64bit

Python: 2.7.10 (formerly 3.6)

On the first line of your Python 3.5 code, as this line:

from __future__ import print_function, division

This will take care of two main differences between Python 3.5 and Python 2.7. There are other s that could be imported to improve compatibility.

and if you want to add the path:

  1. Open System Properties (type it in the start menu, or use the keyboard shortcut Win+Pause)
  2. Switch to the Advanced tab
  3. Click Environment Variables
  4. Select PATH in the System variables section
  5. Click Edit
  6. Add python's path to the end of the list (the paths are separated by semicolons). For example:

    C:\\Windows;C:\\Windows\\System32;C:\\Python27

The Selenium files were installed in my local Python36 folder. After removing all Selenium files from the Python36 folder, I reinstalled Selenium using the .whl files provided at https://pypi.python.org/pypi/selenium (download, then extract and install using pip install <filename>.whl ) and ensured the extracted and installed files were in my /Python27 folder.

This, coupled with a similar procedure for nosetests (shell program used to perform my testing), reset Selenium to execute Python2 code. If anyone has a more elegant solution, please share. Until then, I hope this helps the next poor soul who has to deal with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM