DEV Community

jay shankar k
jay shankar k

Posted on

PYTHON SELENIUM ARCHITECTURE

As we all know, Selenium is an automation tool used for web application testing, and Python is a programming language. Selenium scripts can be written using only programming languages; the most commonly used programming languages are Java and Python.Pyhton and Selenium work together to create automation scripts and code that are used for interactions with web browsers.

This python code sets up a selenium automation script to open a chrome browser and navigate to a specified URL.

"""
This script is a basic example of how to use Selenium with Python to automate web browser action like opening a web page
"""

# Importing the selenium webdriver module to automate web browser interactions.
from selenium import webdriver
#Importing chromedrivermanager to automatically download and manage the chrome driver binary.
from webdriver_manager.chrome import ChromeDriverManager
#Importing the service class from the chrome module of selenium.
from selenium.webdriver.chrome.service import Service


class jay:


    def __init__(self, url):
        self.url = url
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))


    def start(self):
        self.driver.maximize_window()
        self.driver.get(self.url)


# executing main function
if __name__ == "__main__":
    url = "https://www.hotstar.com/in/home?ref=%2Fin"
    jay = jay(url)
    jay.start()

Enter fullscreen mode Exit fullscreen mode

PYTHON VIRTUAL ENVIRONMENTS

Python Virtual environment is an self-contained isolated directory which contains Python and its various modules.
It is used for sand-boking different modules of Python and to test it.

WHY WE USE PYTHON VIRTUAL ENVIROMENT?

  • It is Isolated environment for testing

  • Folder structure for your unique projects

  • Python virtual environments are essential for maintaining clean and reproducible development environments, managing dependencies effectively, and avoiding common issues related to package management and compatibility.

WHY DO WE NEED A VIRTUAL ENVIROMENT?

Imagine a scenario where you are working on two web-based Python projects one of them uses TATAPOWER 4.9 and the other uses TATAPOWER 4.15 (check for the latest ECOPOWER 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.

HOW IT'S WORKS?

  • Creating an environment

  • pip install virtualenv

  • Activating the virtual environment

  • virtual env

  • Scripts\activate

  • Install the required package modules using PIP command

  • deactivate the virtual environment

  • Scripts\deactivate

C:\Users\User>pip install virtualenv
Requirement already satisfied: virtualenv in c:\python312\lib\site-packages (20.26.2)
Requirement already satisfied: distlib<1,>=0.3.7 in c:\python312\lib\site-packages (from virtualenv) (0.3.8)
Requirement already satisfied: filelock<4,>=3.12.2 in c:\python312\lib\site-packages (from virtualenv) (3.14.0)
Requirement already satisfied: platformdirs<5,>=3.9.1 in c:\python312\lib\site-packages (from virtualenv) (4.2.2)

C:\Users\User>python -m venv env

(env) C:\Users\User>cd env

(env) C:\Users\User\env>dir
 Volume in drive C is Windows 10
 Volume Serial Number is D099-1466

 Directory of C:\Users\User\env

06/11/2024  10:56 AM    <DIR>          .
06/11/2024  10:56 AM    <DIR>          ..
06/11/2024  10:56 AM    <DIR>          Include
06/11/2024  10:56 AM    <DIR>          Lib
06/13/2024  12:27 PM               176 pyvenv.cfg
06/13/2024  01:06 PM    <DIR>          Scripts
               1 File(s)            176 bytes
               5 Dir(s)  189,175,468,032 bytes free

(env) C:\Users\User\env>cd scripts

(env) C:\Users\User\env\Scripts>dir
 Volume in drive C is Windows 10
 Volume Serial Number is D099-1466

 Directory of C:\Users\User\env\Scripts

06/13/2024  01:06 PM    <DIR>          .
06/13/2024  01:06 PM    <DIR>          ..
06/13/2024  12:27 PM             2,314 activate
06/13/2024  12:27 PM               984 activate.bat
06/13/2024  12:27 PM            26,199 Activate.ps1
06/13/2024  12:27 PM               393 deactivate.bat
06/13/2024  01:06 PM           108,386 dotenv.exe
06/13/2024  01:06 PM           108,407 normalizer.exe
06/13/2024  01:03 PM           108,395 pip.exe
06/13/2024  01:03 PM           108,395 pip3.12.exe
06/13/2024  01:03 PM           108,395 pip3.exe
06/13/2024  12:27 PM           270,616 python.exe
06/13/2024  12:27 PM           259,352 pythonw.exe
06/13/2024  12:27 PM           789,504 pythonw_d.exe
06/13/2024  12:27 PM           790,016 python_d.exe
              13 File(s)      2,681,356 bytes
               2 Dir(s)  189,174,358,016 bytes free

(env) C:\Users\User\env\Scripts>cd ..

(env) C:\Users\User\env>scripts\activate

(env) C:\Users\User\env>pip list
Package            Version
------------------ -----------
attrs              23.2.0
certifi            2024.6.2
cffi               1.16.0
charset-normalizer 3.3.2
h11                0.14.0
idna               3.7
outcome            1.3.0.post0
packaging          24.1
pip                24.0
pycparser          2.22
PySocks            1.7.1
python-dotenv      1.0.1
requests           2.32.3
selenium           4.21.0
sniffio            1.3.1
sortedcontainers   2.4.0
trio               0.25.1
trio-websocket     0.11.1
typing_extensions  4.12.2
urllib3            2.2.1
webdriver-manager  4.0.1
wsproto            1.2.0

(env) C:\Users\User\env>

(env) C:\Users\User\env>scripts\deactivate
C:\Users\User\env>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)