DEV Community

Bindhu Ashokan
Bindhu Ashokan

Posted on

Python Selenium architecture

Python Selenium Architecture

The Selenium WebDriver architecture facilitates communication between your Python script and the web browser. it consists of four mani components:

1. Selenium Client Library: This is python code you write. It includes the selenium commands you want to execute.

  • you write test scripts using python.

  • It provides APIs like:

    • find_element()
    • click()
    • send_key()

2. WebDriver: WebDriver acts as a bridge between your Python code and the browser.

  • Convert your commands into HTTP requests (JSON format).

  • Send them to browser driver.

3. Browser Drivers: Each browser has its own driver
Eg: chrome driver for Chrome, gecko driver for Firefox.
The driver act as an intermediary, receiving the HTTP requests and translating them into instructions the browser understands.

4. Browser: The end-point where the actual execution happens(eg Chrome, Safari, Edge).

Flow: Python Script - JSON Wire Protocol - Browser Driver - Real Browser.

Working Flow of Selenium Architecture

  1. Python Script sends a command

  2. WebDriver converts it into JSON request

  3. Browser Driver receives request

  4. Browser executes action

  5. Response is sent back

What is the significance of the Python Virtual Environment? Give some examples in support of your answer ?

When developing software with Python, a basic approach is to install Python on your machine, install all your required libraries via the terminal, write all your code in a single .py file or notebook, and run your Python program in the terminal.

This is a common approach for a lot of beginners and many people transitioning from working with Python for data analytics.

This works fine for simple Python scripting projects. But in complex software development projects, like building a Python library, an API, or software development kit, often you will be working with multiple files, multiple packages, and dependencies. As a result, you will need to isolate your Python development environment for that particular project.

Benefits:

You can have multiple environments, with multiple sets of packages, without conflicts among them. This way, different projects’ requirements can be satisfied at the same time.
You can easily release your project with its own dependent modules.

Setting Up a Virtual Environment

Here's a quick guide on how to set up and use a virtual environment:

Install Python Virtual Environment

Pip install virtualenv

Verify Python Virtual Environment

Virtualenv --version

Create Virtual Environment ( To create project folder )

virtualenv
cd

Activate Virtual Environment

Scripts\activate

Deactivate Virtual Environment

Scripts\deactivate

Install Python Selenium Module

pip install selenium

Install Python WebDriver Manager Module

pip install webdriver-manager

Conclusion

The use of Python Virtual Environments is essential for managing project dependencies effectively and ensuring that different projects can coexist peacefully on the same system.

Top comments (0)