- selenium 4.10.0
- Supported Python Versions
- Installing
- Drivers
- Example 0:
- Example 1:
- Example 2:
- Selenium Server (optional)
- Use The Source Luke!
- Contributing
- 1. Installation¶
- 1.2. Installing Python bindings for Selenium¶
- 1.3. Instructions for Windows users¶
- 1.4. Installing from Git sources¶
- 1.5. Drivers¶
- 1.6. Downloading Selenium server¶
- Selenium Client Driver¶
- Supported Python Versions¶
- Installing¶
- Drivers¶
- Example 0:¶
- Example 1:¶
- Example 2:¶
- Selenium Server (optional)¶
- Use The Source Luke!¶
- Setup Selenium with Python and Chrome Driver on Ubuntu & Debian
- Prerequisites
- Step 1: Installing Google Chrome
- Step 2: Installing Selenium and Webdriver for Python
- Example 1: Selenium Python Script with Headless Chrome
selenium 4.10.0
The selenium package is used to automate web browser interaction from Python.
Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer), as well as the Remote protocol.
Supported Python Versions
Installing
If you have pip on your system, you can simply install or upgrade the Python bindings:
Alternately, you can download the source distribution from PyPI (e.g. selenium-4.10.0.tar.gz), unarchive it, and run:
Note: You may want to consider using virtualenv to create isolated Python environments.
Drivers
Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver, which needs to be installed before the below examples can be run. Make sure it’s in your PATH , e. g., place it in /usr/bin or /usr/local/bin .
Failure to observe this step will give you an error selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.
Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.
Example 0:
Example 1:
- open a new Firefox browser
- load the Yahoo homepage
- search for “seleniumhq”
- close the browser
Example 2:
Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example using Python’s standard unittest library:
Selenium Server (optional)
For normal WebDriver scripts (non-Remote), the Java server is not needed.
However, to use Selenium Webdriver Remote or the legacy Selenium API (Selenium-RC), you need to also run the Selenium server. The server requires a Java Runtime Environment (JRE).
Run the server from the command line:
java -jar selenium-server-4.6.0.jar
Then run your Python client scripts.
Use The Source Luke!
Contributing
- Create a branch for your work
- Ensure tox is installed (using a virtualenv is recommended)
- python3.7 -m venv .venv && . .venv/bin/activate && pip install tox
- After making changes, before committing execute tox -e linting
- If tox exits 0 , commit and push otherwise fix the newly introduced breakages.
- flake8 requires manual fixes
- black will often rewrite the breakages automatically, however the files are unstaged and should staged again.
- isort will often rewrite the breakages automatically, however the files are unstaged and should staged again.
1. Installation¶
Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way.
Selenium Python bindings provide a convenient API to access Selenium WebDrivers like Firefox, Ie, Chrome, Remote etc. The current supported Python versions are 3.5 and above.
This documentation explains Selenium 2 WebDriver API. Selenium 1 / Selenium RC API is not covered here.
1.2. Installing Python bindings for Selenium¶
Use pip to install the selenium package. Python 3 has pip available in the standard library. Using pip , you can install selenium like this:
You may consider using virtualenv to create isolated Python environments. Python 3 has venv which is almost the same as virtualenv.
You can also download Python bindings for Selenium from the PyPI page for selenium package. and install manually.
1.3. Instructions for Windows users¶
- Install Python 3 using the MSI available in python.org download page.
- Start a command prompt using the cmd.exe program and run the pip command as given below to install selenium .
C:\Python39\Scripts\pip.exe install selenium
Now you can run your test scripts using Python. For example, if you have created a Selenium based script and saved it inside C:\my_selenium_script.py , you can run it like this:
C:\Python39\python.exe C:\my_selenium_script.py
1.4. Installing from Git sources¶
To build Selenium Python from the source code, clone the official repository. It contains the source code for all official Selenium flavors, like Python, Java, Ruby and others. The Python code resides in the /py directory. To build, you will also need the Bazel build system.
Currently, as Selenium gets near to the 4.0.0 release, it requires Bazel 3.2.0 (Install instructions), even though 3.3.0 is already available.
To build a Wheel from the sources, run the following command from the repository root:
This command will prepare the source code with some preprocessed JS files needed by some webdriver modules and build the .whl package inside the ./bazel-bin/py/ directory. Afterwards, you can use pip to install it.
1.5. Drivers¶
Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver, which needs to be installed before the below examples can be run. Make sure it’s in your PATH , e. g., place it in /usr/bin or /usr/local/bin .
Failure to observe this step will give you an error selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.
Other supported browsers will have their own drivers available. Links to some of the 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 the official documentation.
1.6. Downloading Selenium server¶
The Selenium server is only required if you want to use the remote WebDriver. See the Using Selenium with remote WebDriver section for more details. If you are a beginner learning Selenium, you can skip this section and proceed with next chapter.
Selenium server is a Java program. Java Runtime Environment (JRE) 1.6 or newer version is recommended to run Selenium server.
You can download Selenium server 2.x from the download page of selenium website. The file name should be something like this: selenium-server-standalone-2.x.x.jar . You can always download the latest 2.x version of Selenium server.
If Java Runtime Environment (JRE) is not installed in your system, you can download the JRE from the Oracle website. If you are using a GNU/Linux system and have root access in your system, you can also use your operating system instructions to install JRE.
If java command is available in the PATH (environment variable), you can start the Selenium server using this command:
java -jar selenium-server-standalone-2.x.x.jar
Replace 2.x.x with the actual version of Selenium server you downloaded from the 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 the java command. Similarly, you can provide a relative or absolute path to Selenium server jar file. Then, the command will look something like this:
/path/to/java -jar /path/to/selenium-server-standalone-2.x.x.jar
Selenium Client Driver¶
The selenium package is used to automate web browser interaction from Python.
Home: | https://selenium.dev |
GitHub: | https://github.com/SeleniumHQ/Selenium |
PyPI: | https://pypi.org/project/selenium/ |
IRC/Slack: | Selenium chat room |
Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer), as well as the Remote protocol.
Supported Python Versions¶
Installing¶
If you have pip on your system, you can simply install or upgrade the Python bindings:
Alternately, you can download the source distribution from PyPI (e.g. selenium-4.10.0.tar.gz), unarchive it, and run:
Note: You may want to consider using virtualenv to create isolated Python environments.
Drivers¶
Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver, which needs to be installed before the below examples can be run. Make sure it’s in your PATH , e. g., place it in /usr/bin or /usr/local/bin .
Failure to observe this step will give you an error selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.
Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.
Chrome: | https://chromedriver.chromium.org/downloads |
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/ |
Example 0:¶
from selenium import webdriver browser = webdriver.Firefox() browser.get('http://selenium.dev/')
Example 1:¶
- open a new Firefox browser
- load the Yahoo homepage
- search for “seleniumhq”
- close the browser
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys browser = webdriver.Firefox() browser.get('http://www.yahoo.com') assert 'Yahoo' in browser.title elem = browser.find_element(By.NAME, 'p') # Find the search box elem.send_keys('seleniumhq' + Keys.RETURN) browser.quit()
Example 2:¶
Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example using Python’s standard unittest library:
import unittest from selenium import webdriver class GoogleTestCase(unittest.TestCase): def setUp(self): self.browser = webdriver.Firefox() self.addCleanup(self.browser.quit) def test_page_title(self): self.browser.get('http://www.google.com') self.assertIn('Google', self.browser.title) if __name__ == '__main__': unittest.main(verbosity=2)
Selenium Server (optional)¶
For normal WebDriver scripts (non-Remote), the Java server is not needed.
However, to use Selenium Webdriver Remote , you need to also run the Selenium server. The server requires a Java Runtime Environment (JRE).
Run the server from the command line:
java -jar selenium-server-4.10.0.jar
Then run your Python client scripts.
Use The Source Luke!¶
Setup Selenium with Python and Chrome Driver on Ubuntu & Debian
Selenium is a versatile tool that can be used for automating browser-based tests. It has a wide range of features that make it an ideal choice for automating tests. Selenium can be used to automate tests for web applications and web services. Selenium supports a number of programming languages, including Java, C#, Python, and Ruby.
This makes it possible to write tests in the language that you are most comfortable with. In addition, Selenium has a large user community that provides support and help when needed.
In this blog post, you will learn to set up a Selenium environment on an Ubuntu system. Also provides you with a few examples of Selenium scripts written in Python.
Prerequisites
You must have Sudo privileged account access to the Ubuntu system.
One of the examples also required a desktop environment to be installed.
Step 1: Installing Google Chrome
Use the below steps to install the latest Google Chrome browser on Ubuntu and Debian systems.
- First of all, download the latest Gooogle Chrome Debian package on your system.
wget -nc https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt update
sudo apt install -f ./google-chrome-stable_current_amd64.deb
This will complete the Google Chrome on your Ubuntu or Debian system. This will also create an Apt PPA file for further upgrades.
Step 2: Installing Selenium and Webdriver for Python
We will use a virtual environment for running Python scripts. Follow the below steps to create Python virtual environment and install the required python modules.
- Create a directory to store Python scripts. Then switch to the newly-created directory.
python3 -m venv venv
source venv/bin/activate
Once the environment is activated, You will find the updated prompt as shown below screenshot:
Now use PIP to install the selenium and webdriver-manager Python modules under the virtual environment.
pip install selenium webdriver-manager
Example 1: Selenium Python Script with Headless Chrome
Your system is ready to run Selenium scripts written in Python. Now, create a sample selenium script in Python that fetches the title of a website.
This script will run headless, So you can run it without an X desktop environment. You can simply SSH to your system and run the below example:
- Create a Python script and edit it in your favorite text editor: