Selenium A To Z Using Python

al adnan samial adnan sami
5 min read

Selenium is a powerful tool for automating web browsers. Whether you want to test websites, scrape data, or automate repetitive tasks, Selenium makes it easy and efficient. Today, I’m starting my journey with Selenium using Python.

What is web driver?

A WebDriver is a software component used by Selenium to control and automate a web browser programmatically.

  • One of the component in selenium

  • WebDriver is a package also a module

  • WebDriver is an API

Lets GO ✅ Step-by-Step Guide

Setup and Configure WebDriver in PyCharm

Pre-requisites

  1. Install Python (If not installed, download it from https://www.python.org.)

  2. Install PyCharm

    Download PyCharm from https://www.jetbrains.com/pycharm/ and install it.

  3. Download All Specific Driver

  4. | Chrome: | https://developer.chrome.com/docs/chromedriver | | --- | --- | | Edge: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver | | Firefox: | https://github.com/mozilla/geckodriver | | Safari: | https://webkit.org/blog/6900/webdriver-support-in-safari-10 |

  5. Install Selenium

    Open the terminal in PyCharm -> Go to File > Settings > Project > Python Interpreter -> Click the + icon->Search for selenium, click Install Package.

🚀 Example in Action

Create a New Project in PyCharm

  • Open PyCharm.

  • Go to File > New Project.

  • Set a name and location.

  • Select a virtual environment or existing interpreter.

  • Click Create.

Write a Sample Selenium Script

Our day one simple test case

Test Case Name: Valid Login to OrangeHRM

🧾 Test Case Details

FieldDescription
Test Case IDTC_Login_001
Test TitleVerify that a user can log in with valid credentials
Test PriorityHigh
PreconditionUser is on the OrangeHRM login page
Test Steps
1. Navigate to https://opensource-demo.orangehrmlive.com2. Enter valid username ("Admin")3. Enter valid password ("admin123")4. Click on the Login button
Test DataUsername: AdminPassword: admin123
Expected ResultUser is successfully logged in and the page title is "OrangeHRM"
Actual Result(Printed in console) Either success or failure message
StatusPass / Fail

Note How to Find XPath Locations in Browser (Chrome)

  1. Open Chrome Developer Tools (press F12 or right-click → Inspect).

  2. Right-click the desired element in the HTML.

  3. see the name , id, classname and copy

OUR CODE

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By  # Required for By.XXX

# Path to ChromeDriver
chrome_driver_path = r"E:\Automation_Testing\Web_Driver\Chrome_Driver\chromedriver.exe"

# Setup service and options
service = Service(executable_path=chrome_driver_path)
options = Options()

# Launch browser
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")

# Wait briefly to let elements load (you can improve this with WebDriverWait)
import time
time.sleep(2)

# Use updated locator syntax
driver.find_element(By.NAME, "username").send_keys("Admin")
driver.find_element(By.NAME, "password").send_keys("admin123")
driver.find_element(By.CLASS_NAME, "orangehrm-login-button").click()

actual_title = driver.title
expected_title = "OrangeHRM"
if actual_title == expected_title :
    print("Successfully logged in")

else:
    print("Failed to log in")

driver.close()

Full Code Explanation

from selenium import webdriver

Imports Selenium’s WebDriver module, which is used to automate browser actions (like opening websites, clicking, typing, etc.).

from selenium.webdriver.chrome.service import Service

Imports the Service class used to specify the ChromeDriver path in a structured way. It manages how the driver service runs.

from selenium.webdriver.chrome.options import Options

Imports Options to set various Chrome options (like headless mode, disabling extensions, etc.). Here, it's being initialized, but no specific options are used yet.

from selenium.webdriver.common.by import By

Imports the By class, which provides locator strategies (e.g., By.ID, By.NAME, By.CLASS_NAME, etc.) to find HTML elements.

chrome_driver_path = r"E:\Automation_Testing\Web_Driver\Chrome_Driver\chromedriver.exe"

This sets the absolute path to your chromedriver.exe, which allows Selenium to control the Chrome browser.
🔸 r"" makes it a raw string, so backslashes \ are treated literally.

from selenium.webdriver.chrome.service import Service

service = Service(executable_path=chrome_driver_path)

Initializes the driver service using the path specified above.

from selenium.webdriver.chrome.options import Options

options = Options()

Creates an Options object to configure the Chrome browser. You could add things like options.add_argument('--headless') if needed. Currently, it's empty.

# Initialize the Chrome WebDriver
driver = webdriver.Chrome(service=service, options=options)

Launches a new Chrome browser instance using the specified Service and Options.

# Navigate to the OrangeHRM login page
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")

Opens the OrangeHRM demo login page in the browser.

import time

time.sleep(2)

🔹 Pauses the script for 2 seconds to allow the page elements to fully load.
✅ Useful in small tests, but not reliable.
🔁 Better alternative: WebDriverWait.

driver.find_element(By.NAME, "username").send_keys("Admin")

Finds the input field for the username by its name="username" attribute and types "Admin".

driver.find_element(By.NAME, "password").send_keys("admin123")

🔹 Finds the password field and types "admin123".

from selenium.webdriver.common.by import By

driver.find_element(By.CLASS_NAME, "orangehrm-login-button").click()

🔹 Finds the login button by class name and clicks it.

actual_title = driver.title
expected_title = "OrangeHRM"

🔹 Stores the actual web page title after login, and sets what it should be if login is successful.

if actual_title == expected_title:
    print("Successfully logged in")
else:
    print("Failed to log in")

🔹 Compares actual vs. expected title.

  • If matched → print success.

  • If not → print failure.

driver.close()

🔹 Closes the browser window and ends the test.

🧪 In Summary:

This script is a simple functional test for OrangeHRM:

  • Launches browser

  • Goes to login page

  • Enters valid credentials

  • Clicks login

  • Verifies successful login via page title

  • Closes browser

DAY-2 COMING (XPATH)

0
Subscribe to my newsletter

Read articles from al adnan sami directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

al adnan sami
al adnan sami