Understanding Python Selenium Architecture & Virtual Environment

python dev.to

1. PYTHON SELENIUM ARCHITECTURE

      Selenium is one of the most widely used tools for automating web applications. When we use Selenium with Python, the architecture follows a structured flow that allows script to interact with browser efficiently.
  At a high level, Selenium architecture consistes of 4 main components:

     **1. Selenium Client Library(Python)**
               The test scripts stays at this component. When writing automation code using Python, we use Selenium libraries like webdriver, find_element, etc.
Enter fullscreen mode Exit fullscreen mode
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://google.com")
Enter fullscreen mode Exit fullscreen mode

Here, Python acts as a client that sends instructions.

     **2. WebDriver**
              WebDriver acts as a bridge between Python code and the browser. It translates the commands into a format the browser understands.

             Example:
                 -> driver.get() - opens a webpage
                 -> driver.find_element() - locates elements

      ** 3. Browser Driver**
                Each browser has its own driver such as:
                  1. Chrome -> ChromeDriver
                  2. Firefox -> GeckoDriver
                  3. Egde -> EdgeDriver

              These drivers receive commands from WebDriver and execute them inside the browser.

         **4. Real Browser**
                 The actual browser (Chrome, Firefox) performs the actions like:
                 -> Clicking buttons
                 -> Filling forms
                 -> Navigating pages


## **FLOW OF EXECUTION:**
   1. Python Selenium code is written
   2. Selenium Client library sends commands
   3. WebDriver converts commands into JSON format
   4. Browser Driver receives the request
   5. Browser executes te action
   6. Response is sent back to the script


## **ARCHITECTURAL DIAGRAM:**
Enter fullscreen mode Exit fullscreen mode
   **REAL-TIME EXAMPLE:**
       Let's test a login page:
Enter fullscreen mode Exit fullscreen mode
driver.get("https://example.com/login")
driver.find_element("id", "username").send_keys("user123")
driver.find_element("id", "password").send_keys("pass123")
driver.find_element("id", "login").click()
Enter fullscreen mode Exit fullscreen mode
     In the above code,
      -> python sends commands
      -> WebDriver processes them
      -> ChromeDriver executes them
      -> Browser performs login
Enter fullscreen mode Exit fullscreen mode

2. SIGNIFICANCE OF PYTHON VIRTUAL ENVIRONMENT:

       A python virtual environment is used to create an isolated workspace for the project. It helps manage dependencies separately without affecting othe projects.
Enter fullscreen mode Exit fullscreen mode

** WHY IS IT IMPORTANT?**
In real-world projects, different applications may require different versions of libraries.
Without virtual environment,
- Project A -> Selenium 3
- Project B -> Selenium 4
This will create conflicts amond the versions.

       With virtual environment:
           - Each project has its own dependencies
           - No version clashes
Enter fullscreen mode Exit fullscreen mode

** HOW IT WORKS?**
We'll have to create a virtual environment using the below bash commands:
python -m venv myenv

  • To activate it myenv\Scripts\activate

-Install packages
pip install selenium

Now Selenium is installed only inside this environment that has been created.

 **Advantages:**
      1. Dependency isolation
      2. Cleaner project structure
      3. Easy to manage versions
      4. Avoids conflicts between projects
      5. Improves reproducibility
Enter fullscreen mode Exit fullscreen mode

CONCLUSION:
Puthon Selenium architecture ensures smooth communication between test scripts and browsers using WebDriver and browser drivers. On the other hand, virtual environments help maintain clean, isolates project setups, which is crucial for real-time development and testing. Together, they make automation more efficient, scalable and reliable.

Source: dev.to

arrow_back Back to Tutorials