Table of Contents

Selenium Automation Tutorial

selenium automation tutorial using python with practicle example
selenium automation tutorial using python with practicle example

Selenium Automation Introduction

This blog is written for beginners and intermediate learners who want to learn Selenium Automation using python step by step. The language is kept simple, and every line of code is explained clearly. By the end, you will be able to build real automation scripts confidently.

What is Selenium?

Selenium is an automation tool used to control a web browser using code.For example, the official documentation of Selenium explains WebDriver setup, element locators, and advanced features in detail.ย 

  • Open a browser automatically
  • Click buttons and links
  • Fill forms
  • Login to websites
  • Test web applications
  • It is free and open source
  • Supports multiple programming languages

Prerequisites

  • Basic Python (variables, functions)
  • Basic HTML (input, button, id, class)

Selenium 4+ supoort Selenium Manager – No need to download chromedriver manualy. In this tutorial we will use 4.39.0 selenium .

Selenium Setup and Basic Script In Python

Install Selenium

Install Selenium using pip:

				
					pip install selenium
				
			

Understanding How Selenium Sees Your Webpage

To control your website, Selenium needs to know where to click or type. It does this using “Locators.”

The Problem: You see a “Login” button. Selenium sees HTML code.

The Solution: You must find the unique address of that button in the HTML (usually an id, name, or class).

How to do it :

  1. Open your website in Chrome.
  2. Right-click on the button or text box you want to automate.
  3. Select Inspect.
  4. Look for id="username" or name="submit". These are the identifiers we can use to get element .

First Selenium Script

Let us write our first automation code.

				
					from selenium import webdriver
				
			
				
					driver = webdriver.Chrome()
				
			
				
					driver.get("https://www.google.com")
print(driver.title)
dirver.close()
				
			

Complete First Script Of Browser Automation

				
					from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()
				
			

Locate Web Elements

To interact with elements, Selenium needs to find them.

Common locators:

  • ID : This is the preferred method. IDs are supposed to be unique within a page, making this the fastest and most reliable locator.
  • Name : Often used for form elements (inputs, checkboxes). If multiple elements share same name , selenium select the first one found.
  • Class Name : Used to locate elements based on their CSS class.
  • XPath : It allows you to navigate the HTML structure (up and down) and locate elements by their visible text.
  • CSS Selector : It is faster than XPath and very readable. It uses standard CSS syntax to find elements.

summary: Which one should I use?

Priority Locator Speed Reliability Why?
1 ID Fast High Unique and optimized.
2 CSS Selector Fast High flexible and faster than XPath.
3 XPath Slow Medium Essential for complex navigation or text matching.
4 Name/Class Medium Medium Good fallbacks if ID is missing.

Implicit And Explicit Wait In Selenium Automation

Think of “Waits” in Selenium like telling a robot how much patience it should have. Sometimes websites are slow, and if the robot (Selenium) tries to click a button before it appears, it will crash.

1. Implicit Wait (The “General Rule”)

What it is: A global setting. You set it once, and it applies to every element you try to find in your script.

How it works: If Selenium can’t find an element immediately, it will wait for the time you specified (e.g., 10 seconds) before failing. If the element appears in 2 seconds, it moves on immediately.

Simple Syntax:

				
					# Tell the driver: "Always wait up to 10 seconds if you can't find something."
driver.implicitly_wait(10)
				
			

2. Explicit Wait (The “Specific Rule”)

What it is: A specific setting for one specific element. You use it when you know onepart of the page takes longer to load than orthers.

How it works: You tell Selenium to pause and wait for a specific condition to happen (like “Wait until the Login button is clickable” or “Wait until the loading spinner disappears”).

Simple Syntax:

				
					from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# "Wait up to 10 seconds, BUT only for this specific button to show up."
wait = WebDriverWait(driver, 10)
button = wait.until(EC.presence_of_element_located((By.ID, "submit")))
				
			

Handling Common Web Elements For Browser Automation

Handle Input Form

				
					username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")

username.send_keys("admin")
password.send_keys("123456")

login_btn = driver.find_element(By.ID, "login")
login_btn.click()
				
			

Imagine driver is a robot sitting at a computer. This code gives the robot three specific instructions . given code is used to automate login process.

  1. Look for the boxes named “username” and “password”.
  2. Type “admin” and “123456” into them.
  3. Click the button named “login”.

Code Explanation :

1. Locating the Input Boxes

				
					username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")
				
			

2. Typing the Credentials

				
					username.send_keys("admin")
password.send_keys("123456")
				
			

3. Clicking the Button

				
					login_btn = driver.find_element(By.ID, "login")
login_btn.click()
				
			
HTML for this code

For this code to work, the websiteโ€™s source code (HTML) must look something like this:

				
					<input type="text" id="username">
<input type="password" id="password">
<button id="login">Log In</button>
				
			

If the website uses different IDs (e.g.,ย id="user_email"ย instead ofย id="username"), the script will crash with aย NoSuchElementException.

Scroll Automation

Why would you use this?

โ€œLazy Loadingโ€ (Infinite Scroll):ย Many modern sites (like Twitter, Instagram, or YouTube) donโ€™t load all content at once. You use Scroll Automation to trigger the website to load more items.

Accessing the Footer:ย Sometimes you need to click a โ€œContact Usโ€ link in the footer, but the element is technically โ€œhiddenโ€ because itโ€™s off-screen. While Selenium usually auto-scrolls to elements it clicks .

1. Scroll to bottom

				
					driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
				
			

2. Scroll element into view

				
					element = driver.find_element(By.ID, "footer")
driver.execute_script("arguments[0].scrollIntoView();", element)
				
			

Handle Dropdown

HTML Code Of Our Example

				
					 <select id="country" name="country">
        <option value="">-- Select --</option>
        <option value="us">United States</option>
        <option value="in">India</option>
        <option value="uk">United Kingdom</option>
    </select>
				
			
				
					from selenium.webdriver.support.ui import Select
dropdown = Select(driver.find_element(By.ID, "country"))
dropdown.select_by_visible_text("India")
				
			

Handling Multiple Tab in Selenium Automation

๐Ÿ”น Launch Browser and Open Website

				
					driver = webdriver.Chrome()
driver.get("https://www.python.org")

				
			

๐Ÿ”น Store Main Window Handle

				
					main_window = driver.current_window_handle

				
			

๐Ÿ”น Find the Link to Open in New Tab

				
					about_link = driver.find_element(By.LINK_TEXT, "About")

				
			

๐Ÿ”น Open Link in a New Tab Using JavaScript

				
					driver.execute_script("window.open(arguments[0].href, '_blank');", about_link)
WebDriverWait(driver,10).until(lambda d: len(d.window_handles) > 1)

				
			

๐Ÿ”น Switch to the New Tab

				
					for w in driver.window_handles:
     if w != main_window:
        driver.switch_to.window(w)
        break
print(driver.title)

				
			

๐Ÿ”น Close the Current Tab And Switch Back to Main Tab

				
					driver.close()
driver.switch_to.window(main_window)
print(driver.title)

				
			

Complete Code

				
					driver = webdriver.Chrome()
driver.get("https://www.python.org")

main_window = driver.current_window_handle

about_link = driver.find_element(By.LINK_TEXT, "About")
driver.execute_script("window.open(arguments[0].href, '_blank');", about_link)

WebDriverWait(driver,10).until(lambda d: len(d.window_handles) > 1)

for w in driver.window_handles:
    if w != main_window:
        driver.switch_to.window(w)
        break

print(driver.title)
driver.close()
driver.switch_to.window(main_window)

print(driver.title)
				
			

Selenium Automation with Custom Chrome Profile

Benefits Of Using Custome Profile In Selenium Automation

  • Cookies are stored inside the custom profile.
  • You can stay logged in across runs
  • This reduces repeated login steps
  • Very useful for real-world selenium automation
  • Custom profile supports long sessions.
  • Suitable for scraping, monitoring, or testing.
  • Improves reliability in advanced selenium automation.
  • we can use extention in custom profile.

Important Note : Create new fresh chrome profile to avoid conflictย for selenium automation.

๐Ÿ”น Import Required Modules

				
					from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import os


				
			

๐Ÿ”น Create a Separate Chrome Profile

				
					options = Options()
profile_path = r"C:\Users\<Your_Username>\AppData\Local\Google\Chrome\User Data\<Your_Profile_Name>"
os.makedirs(profile_path, exist_ok=True)

				
			

๐Ÿ”น Attach Profile to Chrome And Add Stability Options

				
					options.add_argument(f"--user-data-dir={profile_path}")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

				
			

๐Ÿ”น Start Chrome Driver

				
					driver = webdriver.Chrome(options=options)
print(driver.current_url)
driver.quit()

				
			

Complete Code For Custome Profile

				
					from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import os

options = Options()
profile_path = r"C:\Users\<Your_Username>\AppData\Local\Google\Chrome\User Data\<Your_Profile_Name>"
os.makedirs(profile_path, exist_ok=True)

options.add_argument(f"--user-data-dir={profile_path}")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome(options=options)
print(driver.current_url)
driver.quit()
				
			
Scroll to Top