Selenium is a versatile tool, which is widely used for automating browser-based tests. It 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.
This tutorial will help you to configure the environment for Selenium with Python and Chrome on Fedora. We will discuss an example written in Python.
Prerequisites
Assuming you have access to a Fedora system with a Sudo privileged account.
This tutorial can be run with GUI access or shell access only.
Step 1 – Installing Google Chrome
You can use firefox or Google Chrome web browser to run your selenium test cases. In this article, we will discuss examples with the Google Chrome web browser.
So, Let’s install Google chrome first. Enable the google-chome repository with the below-mentioned commands:
sudo dnf install fedora-workstation-repositories
sudo dnf config-manager --set-enabled google-chrome
Now, install the latest google chrome stable web browser:
sudo dnf install google-chrome-stable
Google Chrome will be installed on your Fedora system.
Step 2 – Setup Python Environment
We will create a virtual environment for running our Python test cases. Follow the below steps to create Python virtual environment, and install the required modules.
- Installing Python and its virutal environent module.
sudo dnf install python3 python3-virtualenv
- Create a directory for contianing python environment and scripts..
mkdir tests && cd tests
- Create virutal environment
python3 -m venv venv
source venv/bin/activate
- Installing selenium and webdriver manager using PIP under the virtual environemnt.
pip install selenium webdriver-manager
Step 3 – Running an Example with Selenium Python
The Python virtual environment is ready to run Selenium scripts. Let’s run an example script, that opens a website in a headless (Useful for remote ssh access) google chrome browser and prints the title of the website.
Make sure the Python virtual environment is active. you can identify that using the terminal prompt. Now create a Python script and edit it in a text editor.
nano test1.py
Copy-paste the below snippet to file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) driver.get("https://python.org") print(driver.title) driver.close() |
Press CTRL +O
to write changes and then press CTRL + X
to exit from the editor.
Now, run your Python script:
python test1.py
You will see the output something like below:
At the first run, the script will download the latest chromedriver
and place it in your system to use for the next executions.
In the output, you can see the title of the given website is printed on the screen.
Conclusion
Selenium is a popular tool among website testers for running automatic test cases. In this tutorial, we have discussed configuring the Selenium environment with Python scripts.