Selenium
What is Selenium?
Selenium is an open-source automation framework used to automate web browsers. It allows you to interact with web applications just like a real user—clicking buttons, filling out forms, navigating pages, and even running automated tests.
It supports multiple programming languages like Python, Java, C#, and JavaScript and works with popular browsers like Chrome, Firefox, Edge, and Safari.
Use Case: Selenium is mainly used for web application testing, web scraping, and automating repetitive browser tasks.
How Does Selenium Work?
Selenium works by interacting with web elements through WebDriver, which acts as a bridge between your automation script and the web browser.
Basic Workflow of Selenium:
Write a script using Python (or another language).
Use Selenium WebDriver to control the browser.
Launch the browser (e.g., Chrome, Firefox).
Perform actions (click, type, navigate, etc.).
Validate results (check page title, element text, etc.).
Close the browser when done.
Where is Selenium Used?
🔹 Web Application Testing – Automate UI testing. 🔹 Web Scraping – Extract data from web pages. 🔹 Automating Repetitive Tasks – Auto-login, form filling, etc. 🔹 CI/CD Pipelines – Used in DevOps workflows. 🔹 Performance Monitoring – Automate page load testing.
1. Install Selenium
Open your terminal or command prompt and install Selenium using pip:
2. Download WebDriver
Selenium needs a driver to control the browser. Download the appropriate driver for your browser:
Chrome: Chromedriver
Firefox: Geckodriver
Edge: Microsoft Edge WebDriver
Ensure the driver is placed in a directory accessible by your system PATH.
Step 3: Launch a Browser with Selenium
Once installed, let's open a browser using Selenium.
Example: Open Google in Chrome
🔹 .get(url): Opens the given URL.
🔹 .quit(): Closes the browser.
Step 4: Interact with Web Elements
To interact with elements on a webpage, we need locators. The most common locators are:
ID
Name
Class Name
Tag Name
CSS Selector
XPath
Example: Search in Google
🔹 find_element(By.NAME, "q"): Finds the search box by name attribute.
🔹 send_keys("Selenium Python"): Inputs text into the search box.
🔹 Keys.RETURN: Simulates pressing the Enter key.
Step 5: Handling Clicks and Buttons
Many websites require interaction with buttons and links.
Example: Clicking a Button
Here, we use .click() to click the Google search button.
Step 6: Handling Dropdowns and Forms
Some websites have dropdown menus and forms that need interaction.
Example: Selecting from a Dropdown
🔹 Select(driver.find_element(By.ID, "dropdown_id")): Finds the dropdown element.
🔹 .select_by_visible_text("Option 1"): Selects the option with the given text.
Step 7: Handling Alerts and Pop-ups
Web applications sometimes display pop-ups or alerts.
Example: Handling an Alert
🔹 .switch_to.alert: Switches to the alert window.
🔹 .accept(): Clicks the OK button.
Step 8: Working with Frames and Windows
Some websites have iframes or open new windows.
Switching to an iFrame
Switching Between Windows
🔹 .switch_to.frame(): Switches to an iframe.
🔹 .switch_to.window(): Switches to another tab.
Step 9: Implicit and Explicit Waits
Selenium can wait for elements to load before interacting.
Implicit Wait
Explicit Wait
🔹 implicitly_wait(): Waits for elements to appear.
🔹 WebDriverWait(): Waits for a specific condition.
Step 10: Running Tests in Headless Mode
You can run Selenium without opening a browser (useful for automation).
Example: Headless Chrome
🔹 options.headless = True: Runs the browser in the background.
Step 11: Running Selenium Tests in PyTest
If you want to write automated tests, use pytest.
Example: Writing a Test Case
Run the test using:
Step 12: Scraping Data with Selenium
Although BeautifulSoup is used for web scraping, Selenium can also extract data.
Example: Scraping Titles
🔹 find_elements(): Finds multiple elements.
🔹 .text: Extracts text from elements.
Step 13: Running Selenium on a Cloud Server (CI/CD Integration)
If you want to run Selenium tests automatically in the cloud, you can use:
Selenium Grid
Dockerized Selenium
Cloud Testing Platforms (e.g., BrowserStack, Sauce Labs)
Example Docker command to run Selenium Grid:
Step 14: Advanced Selenium Concepts
Capturing Screenshots
File Upload
Mouse Actions
Example: Open Google and Search for "Selenium Python"
🔹 WebDriver interacts with Google Chrome.
🔹 Finds elements using locators like By.NAME.
🔹 Simulates user input (send_keys() and RETURN).
Last updated