DEV Community

gokila selvaraj
gokila selvaraj

Posted on

Python Selenium Architecture

*Python Selenium Architecture *

The Selenium architecture defines how your Python test script interacts with the browser using Selenium WebDriver.

It consists of four main components:

Components of Selenium Architecture

  1. *Selenium Client Library *
    • Selenium provides libraries for multiple languages: Python, Java, C#, Ruby, JavaScript, etc.
    • Testers write test scripts using the Python Selenium library (selenium package).
    • Example:
    • from selenium import webdriver
    • driver = webdriver.Chrome()
    • driver.get("https://www.google.com")
    This script is written in Python, but Selenium converts it into a form the browser understands.

  2. JSON Wire Protocol / W3C WebDriver Protocol

• Acts as a communication bridge between your Python script and the browser driver.
• Your Python commands are converted into JSON (JavaScript Object Notation) format and sent to the browser driver via HTTP.
• Example:
o Python command: driver.get("https://www.google.com")
o Is converted to a JSON request, sent to the ChromeDriver, telling it to open the page.

  1. Browser Drivers

• Each browser has its own driver that communicates between Selenium and the actual browser:
o Chrome → ChromeDriver
o Firefox → GeckoDriver
o Edge → EdgeDriver
o Safari → SafariDriver
• The driver receives commands in JSON format from Selenium and translates them into native browser actions.
• Communication happens through HTTP requests on a specific port.

  1. Real Browsers

• Finally, the browser executes those actions:
o Open a URL
o Click a button
o Enter text
o Capture screenshots
• The browser performs the operation and sends the response (status or error message)
back through the same route:

• Browser → Browser Driver → JSON Response → Selenium Client Library → Python Script
Selenium Architecture Flow Diagram
Python Test Script

Selenium Client Library (Python Bindings)

JSON Wire / W3C WebDriver Protocol

Browser Driver (e.g., ChromeDriver)

Web Browser (e.g., Chrome)

Response Path (Back):

Web Browser → Browser Driver → JSON Response → Selenium Library → Python Script

driver.find_element("id", "loginBtn").click()

step-by-step:

  1. The Python Selenium library sends a command: click element with ID 'loginBtn'.
  2. It is converted into JSON format and sent via HTTP to the browser driver.
  3. ChromeDriver receives it and tells the Chrome browser to perform a click.
  4. The browser performs the click and sends a success response back to Selenium.

Significance of the Python Virtual Environment

A Python virtual environment is an isolated workspace that allows you to install and manage Python packages independently for each project — without affecting your system-wide (global) Python installation.

It helps developers and testers maintain different versions of Python libraries for multiple projects on the same system.

Example:

Project A (OrangeHRM Automation): needs selenium==4.10.0

Project B (Banking Automation): needs selenium==4.5.0

If both use the same global Python, there’ll be a version conflict.
With virtual environments, both can coexist peacefully:

Component-Role
Selenium Client Library-Converts Python commands into JSON
JSON Wire/W3C Protocol-Defines the communication standard
Browser Driver-Translates JSON commands into browser actions
Browser-Executes user actions (click, type, etc.)

Benefit:

Benefit-Description
Isolation-Keeps dependencies separate per project
Reproducibility-Ensures same package versions across systems
Stability-Prevents conflicts with global Python packages
Portability-Easy to share via requirements.txt

Top comments (0)