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:
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:
from selenium import webdriver
# Path to chromedriver (if not in PATH, specify the absolute path)
driver = webdriver.Chrome()
# Open a website
driver.get("https://www.google.com")
# Close the browser
driver.quit()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.google.com")
# Find the search box using name locator
search_box = driver.find_element(By.NAME, "q")
# Type in the search box and press Enter
search_box.send_keys("Selenium Python" + Keys.RETURN)
# Close the browser after 5 seconds
import time
time.sleep(5)
driver.quit()
driver.find_element(By.NAME, "btnK").click()
from selenium.webdriver.support.ui import Select
dropdown = Select(driver.find_element(By.ID, "dropdown_id"))
dropdown.select_by_visible_text("Option 1")
alert = driver.switch_to.alert
alert.accept() # Clicks OK
driver.switch_to.frame("iframe_id")
driver.switch_to.window(driver.window_handles[1])
driver.implicitly_wait(10) # Waits up to 10 seconds
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
action.move_to_element(element).perform()
pythonCopyEditfrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Start a browser session
driver = webdriver.Chrome()
# Open Google
driver.get("https://www.google.com")
# Find the search box and type a query
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium Python" + Keys.RETURN)
# Wait and close browser
import time
time.sleep(5)
driver.quit()