1.Installation

1.1.Introduction

Selenium Python bindings provides a simple API to write functional/acceptancetests using Selenium WebDriver. Through Selenium Python API you can access allfunctionalities of Selenium WebDriver in an intuitive way.

Selenium Python bindings provide a convenient API to access Selenium WebDriverslike Firefox, Ie, Chrome, Remote etc. The current supported Python versions are3.5 and above.

This documentation explains Selenium 2 WebDriver API. Selenium 1 / Selenium RCAPI is not covered here.

1.2.Installing Python bindings for Selenium

Usepip to install the seleniumpackage. Python 3 has pip available in thestandard library. Usingpip, you caninstall selenium like this:

pipinstallselenium

You may consider usingvirtualenv tocreate isolated Python environments. Python 3 hasvenv which is almost the same asvirtualenv.

You can also download Python bindings for Selenium from thePyPI page forselenium package. and installmanually.

1.3.Instructions for Windows users

  1. Install Python 3 using theMSI available in python.org download page.

  2. Start a command prompt using thecmd.exe program and run thepipcommand as given below to installselenium.

    C:\Python39\Scripts\pip.exeinstallselenium

Now you can run your test scripts using Python. For example, if you havecreated a Selenium based script and saved it insideC:\my_selenium_script.py, you can run it like this:

C:\Python39\python.exeC:\my_selenium_script.py

1.4.Installing from Git sources

To build Selenium Python from the source code, clonethe official repository. It contains the source code forall official Selenium flavors, like Python, Java, Ruby and others. The Pythoncode resides in the/py directory. To build, you will also need theBazel build system.

Note

Currently, as Selenium gets near to the 4.0.0 release, it requires Bazel 3.2.0(Install instructions), even though 3.3.0is already available.

To build a Wheel from the sources, run the following command from the repositoryroot:

bazel//py:selenium-wheel

This command will prepare the source code with some preprocessed JS files neededby some webdriver modules and build the.whl package inside the./bazel-bin/py/ directory. Afterwards, you can usepip to install it.

1.5.Drivers

Selenium requires a driver to interface with the chosen browser. Firefox, forexample, requiresgeckodriver, which needs to be installedbefore the below examples can be run. Make sure it’s in yourPATH, e. g.,place it in/usr/bin or/usr/local/bin.

Failure to observe this step will give you an errorselenium.common.exceptions.WebDriverException: Message: ‘geckodriver’executable needs to be in PATH.

Other supported browsers will have their own drivers available. Links to some ofthe more popular browser drivers follow.

Chrome:

https://sites.google.com/chromium.org/driver/

Edge:

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Firefox:

https://github.com/mozilla/geckodriver/releases

Safari:

https://webkit.org/blog/6900/webdriver-support-in-safari-10/

For more information about driver installation, please refer theofficialdocumentation.

Starting from version4.6.0 (November 4, 2022)selenium comes withSelenium Manager packed in distribution.

Selenium Manager is a new tool that helps to get a working environmentto runSelenium out of the box:

  • automatically discovers, downloads, and caches thedriversrequired by Selenium when thesedrivers are unavailable;

  • automatically discovers, downloads, and caches thebrowsersdriven with Selenium (Chrome, Firefox, and Edge)when thesebrowsers are not installed in the local system.

For example, to see the result ofSelenium Manager workjust run any selenium script without previous driver setupand explore~/.cache/selenium.

More aboutSelenium Manager you can read in thedocumentationandblog.

1.6.Downloading Selenium server

Note

The Selenium server is only required if you want to use the remoteWebDriver. See theUsing Selenium with remote WebDriver section for moredetails. If you are a beginner learning Selenium, you can skip this sectionand proceed with next chapter.

Selenium server is a Java program. Java Runtime Environment (JRE) 1.6 or newerversion is recommended to run Selenium server.

You can download Selenium server 2.x from thedownload page of selenium website. The file name should be something likethis:selenium-server-standalone-2.x.x.jar. You can always download thelatest 2.x version of Selenium server.

If Java Runtime Environment (JRE) is not installed in your system, you candownload theJRE from the Oracle website. If youare using a GNU/Linux system and have root access in your system, you can alsouse your operating system instructions to install JRE.

Ifjava command is available in the PATH (environment variable), you can startthe Selenium server using this command:

java-jarselenium-server-standalone-2.x.x.jar

Replace2.x.x with the actual version of Selenium server you downloaded fromthe site.

If JRE is installed as a non-root user and/or if it is not available in the PATH(environment variable), you can type the relative or absolute path to thejavacommand. Similarly, you can provide a relative or absolute path to Seleniumserver jar file. Then, the command will look something like this:

/path/to/java-jar/path/to/selenium-server-standalone-2.x.x.jar