Introduction to Selenium
Selenium is free, multirated and powerful Web Browsers automation tool. It actually imitates
the steps of performing real user’s actions interacting to web pages thus making widely
implemented in web applications testing activities like web scraping.
Selenium supports multiple programming languages such as Python, Java, C# and JavaScript.
In this tutorial, we will use Selenium with Python.
We can use selenium with various browsers like Google Chrome, Mozilla Firefox, Safari,
and Microsoft Edge, through browser-specific drivers known as WebDrivers.
Setting Up the Python Environment
Before installing Selenium, make sure you have Python installed. Go through these steps to
set up your Python environment:
a) Download python using this link – https://www.python.org/downloads/
b) While installing Python please check the box “Add Python to PATH”
c) Verify Installation: Open your command line/terminal and type:
“`bash
`python –version
`
You will get the Python version, for example, `Python 3.x.x`.
d) To check pip installation, run the below command:
“`
pip –version
“`
If it doesn’t give the installed version, please refer to this guide: https://pip.pypa.io/en/stable/installation/
Useful Links – Selenium With Python Training Course | Selenium With Java or Python – Which is Best
Installing Selenium with pip
The next step is to install Selenium using pip. Just open your terminal/command prompt and
run the following command.
“`bash
pip install selenium
“`
This will download and install the latest version of the Selenium package from the Python
Package Index (PyPI). You can verify the installation by running:
“`bash
pip show selenium
“`
This command will show the version of Selenium that has been installed.
Installing WebDriver
Selenium requires a WebDriver to access the browser. Every browser type (Chrome, Firefox,
Edge, etc.) has its WebDriver. Below are steps to download the WebDriver for Google
Chrome.
4.1. Downloading ChromeDriver for Google Chrome
a) Check Chrome Version: Click on three dots on your right most corner–>settings–>About
Chorme(example: 115.0.5790.98).
b) To download compatible ChromeDriver go to: <https://sites.google.com/a/chromium.org/
chromedriver/downloads> Download appropriate to your Chrome version:
c) Extract the chromedriver file
d) You might need to move chromedriver.exe from the downloaded zip (from step 2 above)
to a folder; place it in a location for example, C:\chromedriver on Windows or `/usr/local/bin`
on mac/osx/linux.
4.2. Alternative Gecko Driver to use Firefox
When we opt to use Firefox instead of Chrome, you require Gecko Driver :
– Download it from the [GeckoDriver releases page](https://github.com/mozilla/geckodriver/
releases).
– Install it similarly to ChromeDriver.
Configuring the WebDriver Path
After downloading the WebDriver, we have to give Selenium path. We can do this in two
ways:
Option 1: Set Path in the Script
You can set the path of the WebDriver directly in your Python script:
“`python
from selenium import webdriver
“`
driver = webdriver.Chrome(executable_path=”path/to/chromedriver”) # Replace with actual
path
driver.get(“https://www.google.com”)
“`
Option 2: Set Path via Environment Variables
You can also add the path to the WebDriver to your system’s PATH environment variable:
– Windows: Add the WebDriver folder path to the System Environment Variables.
– macOS/Linux: Add the WebDriver path to your `~/.bashrc` or `~/.zshrc` file using the
`export PATH` command.
Once set, you can simply initialize the WebDriver without specifying the path in your script:
“`python
from selenium import webdriver
driver = webdriver.Chrome() # No need for executable_path
driver.get(“https://www.google.com”)
Useful Links – Selenium With Python Training Course | Selenium With Java or Python – Which is Best
Let’s start the automation fun with Google search
The configuration part is completed, let’s start writing a simple Python script to interact with
a website using Selenium.
Example: Automating a Google Search
1. Open a new Python file (e.g., `google_search.py`).
2. Write the following code:
“`python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
# Initialize WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())
# Open Google
driver.get(“https://www.google.com”)
# Check the title contains “Google”
assert “Google” in driver.title
# Find the search box by name
search_box = driver.find_element(“name”, “q”)
# Send the search query
search_box.send_keys(“Selenium Python tutorial”)
# Press the Enter key
search_box.send_keys(Keys.RETURN)
# Wait a few seconds for results to load
driver.implicitly_wait(5)
# Close the browser window
driver.quit()
It will open Google, type a search query, hit Enter, and then close the browser.
Handling Common Problems
When working with Selenium, you may have run into several common problems. Here are
some of them:
Problem 1: WebDriver Not Found
– Ensure the WebDriver is installed and the path is properly set.
Problem 2: ElementNotFoundException
– Choosing the right web element selector is very crucial while scripting if it gives
ElementNotFoundException, make sure you are using correct selector or try adding
‘WebDriverWait’ to wait for the element to become available.
Problem 3: ChromeDriver Version Mismatch
– Make sure your version of ChromeDriver matches the version of Google Chrome you are
running.
Useful Links – Selenium With Python Training Course | Selenium With Java or Python – Which is Best
Headless Mode (Optional)
To run Selenium in headless mode:
“`python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument(“–headless”) # Run Chrome in headless mode
# Initialize WebDriver in headless mode
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
driver.get(“https://www.google.com”)
print(driver.title) # Output the title of the page
driver.quit()
“`
This script will run Chrome without opening a browser window, and you can interact with it
programmatically.
Conclusion and Next Steps
Congratulations! Here are some next steps to continue your learning:
– Explore more WebDriver commands – [official documentation](https://www.selenium.dev/
documentation/en/webdriver/)
– Develop More Advanced Scripts: Expand your scripts using Selenium to automate testing
tasks or web scraping.
– Implement Selenium in Testing Frameworks: Implement Selenium in Python testing
frameworks like unit test or pytest to automatically test web applications.
– Try Other Browsers: Try your scripts with other browsers (Firefox, Edge) by simply
changing the WebDriver and modifying your script appropriately.
With that, you’ve covered all of the basics in using Python with Selenium to automate a
browser. Have fun automating!