Python Selenium Architecture
Published: 2024-04-05 15:14:24

**What is Selenium?**
Selenium is a web automated testing tool that supports cross-browser testing across various operating systems. Selenium supports Python, JavaScript, JAVA, C# and Ruby.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8j8s7rz4rti6aihxxex.png)
                _**Fig: Selenium WebDiver Architecture**_

**Selenium Web-Driver:**
Selenium WebDriver is a collection of APIs that allows communication between your test script and the web browser.
Selenium WebDriver API provides an understandable programming interface. WebDriver interacts with the browser using the browser’s native support for automation. For each browser(e.g. Chrome, Firefox).JAVA and C# languages are mostly preferred to work with Selenium.

**Selenium Client Library:**
Selenium supports various programming languages, including Python. The Selenium client library for Python provides bindings to interact with the WebDriver. The selenium client library can be divided into two groups:

**1. Web Driver protocol clients:**
     They are thin wrappers around WebDriver protocol HTTP calls. 

  **2. WebDriver-based tools :**
     These are higher-level libraries that allow us to work with 
     WebDriver automation. 

In Python, you can install the Selenium library using a package manager like 'pip’. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gdn45v5bqmhu8n2cceb.png)

**Browser Drivers:**
Browser drivers act as a bridge between the Selenium libraries and the browsers. They help to run Selenium commands on the browser. For e.g., the Chrome Driver executable is used to communicate with the Chrome browser.

**JSON Wire Protocol:**
The communication between the Selenium WebDriver and the browser driver is facilitated by the JSON Wire Protocol. This protocol defines a RESTful web service using HTTP requests and responses in JSON format.

**Web Browser:**
The web browser is the application being automated. Selenium supports various browsers, including Chrome, Firefox and Safari, Internet Explorer and operating systems like Windows, Mac OS, Linux.

**Selenium WebDriver Feature:**
1.Selenium WebDriver architecture is designed to support cross- 
  browser testing and parallel testing.
2.It is open-source, supports many languages, and is compatible 
  with many operating systems.
3.It also supports integration with testing frameworks like TestNG 
  to improve automation testing and reporting.
4.Selenium has strong community support which makes 
  troubleshooting pretty easy.
5.Selenium Architecture enables us to implement user gestures like 
  the mouse cursor and keyboard actions like click, double click, 
  drag, and drop, click and hold, etc.
6.The architecture of Selenium enables us to simulate advanced 
  browser interactions like clicking the browser’s back and front 
  buttons.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nj4kw09rc52wns3oqbs.png)


**Fig: Selenium WebDriver Feature**






**What is significance of the Python Virtual Environment?**
Python virtual environments allow developers to control software dependencies in Python code. They’re useful ways of ensuring that the correct package/library versions are consistently used every time the software runs. Virtual environments also help ensure that the results from running code are reproducible.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbioovf3o67navuh1fkj.png)


A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use.
A scenario where you are working on two web-based Python projects one of them uses Django 4.0 and the other uses Django 4.1 (check for the latest Django versions and so on). In such situations, we need to create a virtual environment in Python that can be really useful to maintain the dependencies of both projects.

**Create Virtual Environment in Python**
We use a module named virtualenv which is a tool to create virtual environments in Python, isolated from system environment Python.

- **Steps for creating Virtual Environment in Python**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dle8rc05v3lugp8yx82k.png)

**1. Step 1 - Install virtualenv:**
    pip install virtualenv  


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6by1hyg70ppfp1ade83c.png)



**2. Step 2 - Creating a Virtual Environment:**
      **1. Using venv:**
         python.exe -m venv new-env  
        **2. Using virtualenv:**
              virtualenv new_env 
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l4a8jo8ui3gva5y9j2a.png)


 **3. Step 3 - Activate the virtual environment:**
        On Windows, Press Enter after typing
        .\new-env\Scripts\activate


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gck5rkle7g6ytnek0wo1.png)


**How to fix it:**
   Step 1 - Run Powershell as administrator
   Step 2 - Execute the following command:
     "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned"  
   Step 3 - Enter 'Y'


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p80jtillw8dni050b0u7.png)

   Step 4 - Managing Packages Using pip
       **pip install requests** 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nllz61u9i4ecv3ncw7e.png)