Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Selenium WebDriver Architecture and Operation Principles

Tech May 13 3

Selenium WebDriver operates through a three-tier architecture analogous to a taxi service:

  • The test automation script acts as the passenger, specifying navigation and actions.
  • Browser drivers function as the taxi drivers, interpreting commands and controlling browser behavior.
  • Web browsers serve as the vehicles, executing actual user interface operations.

At the technical level, these components correspond to:

  1. WebDriver APIs available in multiple languages (Java, Python, C#).
  2. Browser-specific executables (chromedriver.exe, geckodriver.exe, IEDriverServer.exe).
  3. Standard web browsers like Chrome, Firefox, and Internet Explorer.

Communication occurs via HTTP requests between the test script and the browser driver server. Each command from the automation script is packaged into an HTTP request sent to the driver's embedded HTTP server, which then translates and forwards instructions to the browser.

The underlying protocol used is JSON Wire Protocol, which defines standardized JSON structures for request/response bodies over HTTP. Common HTTP methods include:

  • GET: Retrieve information such as page titles.
  • POST: Execute actions like element location or click operations.

Response status codes provide specific feedback:

  • Status 7: NoSuchElement
  • Status 11: ElementNotVisible
  • Status 200: Success

To illustrate, starting chromedriver initializes a server on port 9515:

chromedriver
Starting ChromeDriver 2.39.562713 (dd642283e958a93ebf6891600db055f1f1b4f3b2) on port 9515
Only local connections are allowed.

A typical session initiation involves sending a POST request to http://localhost:9515/session with capability specifications:

import requests
import json

session_url = 'http://localhost:9515/session'
session_data = {
    "capabilities": {
        "firstMatch": [{}],
        "alwaysMatch": {
            "browserName": "chrome",
            "platformName": "any"
        }
    },
    "desiredCapabilities": {
        "browserName": "chrome",
        "platform": "ANY"
    }
}
r_session = requests.post(session_url, json=session_data)
print(json.dumps(r_session.json(), indent=2))

Following session creation, subsequent operations target specific endpoints:

Opening a URL:

url = 'http://localhost:9515/session/{}/url'.format(session_id)
payload = {"url": "https://www.baidu.com"}
requests.post(url, json=payload)

Finding elements:

url = 'http://localhost:9515/session/{}/element'.format(session_id)
payload = {"using": "css selector", "value": ".postTitle a"}
requests.post(url, json=payload)

Interacting with elements:

url = 'http://localhost:9515/session/{}/element/{}/click'.format(session_id, element_id)
payload = {"id": element_id}
requests.post(url, json=payload)

Complete example demonstrating basic browser control:

import requests
import time

capabilities = {
    "capabilities": {
        "alwaysMatch": {
            "browserName": "chrome"
        },
        "firstMatch": [{}]
    },
    "desiredCapabilities": {
        "platform": "ANY",
        "browserName": "chrome",
        "version": "",
        "chromeOptions": {
            "args": [],
            "extensions": []
        }
    }
}

# Launch browser
res = requests.post('http://127.0.0.1:9515/session', json=capabilities).json()
session_id = res['sessionId']

# Navigate to page
requests.post('http://127.0.0.1:9515/session/{}/url'.format(session_id),
             json={"url": "http://www.baidu.com"})
time.sleep(3)

# Close session
requests.delete('http://127.0.0.1:9515/session/{}'.format(session_id))

Understanding these fundamentals aids debugging and provides insight into low-level browser automation mechanics.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.