Reading list Switch to dark mode

    Implementation of Selenium with Python

    Updated 28 February 2023

    What is Selenium?

    Selenium is an open-source web automation framework that can be used to automate web browser interactions. It is primarily used for automating web applications for testing purposes, but is certainly not limited to just that. It can also be used for web scraping.

    What is Python?

    Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

    Selenium with Python can be used to automate web browser interactions. With Selenium, We can write test scripts in Python code to open a web browser, navigate to a page, and interact with elements on the page. We can also use Python to scrape data from web pages. Selenium has a set of methods that can be used to select and interact with elements on web pages.

    Install Python using APT

    APT, or Advanced Package Tool is the default package manager that we will find on Ubuntu. We can download the Python package from the official Ubuntu repository. Here’s how to do it:

    Start your headless eCommerce
    now.
    Find out More

    1.Open up terminal by pressing Ctrl + Alt + T.

    2.Update our local system’s repository list by entering the following command:

    $ sudo apt update

    3.Download the latest version of Python

    $ sudo apt install python3

    4.Check the version of Python.

    $ python --version

    Output-

    Python 2.7.18

    Install pip

    1.Install pip

    $ sudo apt install python3-pip

    2.Check the version of pip

    $ pip3 --version

    Output-

    pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

    Install Selenium

    Step – 1 :- Install Selenium in Python

    Run the python command to get started. If we are using an IDE, simply create a new Python file.

    Use the “pip” command in our terminal to install the Selenium package for Python.

    $ pip install selenium

    Step – 2 :- Download web driver

    Selenium works with all major browsers, including Chrome, Firefox, Internet Explorer, Safari, and Edge. We are using Chrome for this tutorial. 

    Check the web browser version we are using before downloading the web driver.

    download-webdriver

    Go to the Chrome web driver website. Here, we can find the version of Chrome that we are using and then click the corresponding version of the Chrome web driver. 

    Choose the option for the system we are using and make sure to download the zip file for our system solely; otherwise, it might lead to misunderstanding. 

    Once downloaded, extract the file and place it in a specific location on the system. 

    We recommend placing it in the safe directory, which is not often accessed like ‘Program files’ in C-disk. 

    Connecting Selenium with Python and browser

    To test Selenium installation, type ‘import Selenium’ in a Python window or Python script. If we don’t get any errors,.

    Let’s import the necessary module –

    $ from Selenium import webdriver

    webdriver:

    This built-in Selenium package function drives the actions; it connects our script to the browser and can do all the actions we describe

    2.Create an object (every variable created in Python becomes an object) and add the path to the Chrome driver file as an attribute.

    $ driver = webdriver.Chrome('web_path/chromedriver.exe')

    Use the command’ webdriver.Chrome()’. The location of the Chrome driver’s “exe” file must be specified in this command.

    3.After this, we can use the ‘driver.get()’ command to open any website.

    $ driver.get("https://web_link.com/")

    Our browser should automatically open the site URL when we run the script.

    So, this is how we connect Selenium, Python, and the web browser.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home