Browser Automation in Python With Selenium | Python Project

by | Jan 31, 2022 | Coding, Python

Introduction of the Project

Is it your routine to open chrome and search using it? If yes, then it’s time to try something new. Today, we will teach you how to operate your chrome browser using a piece of code in python. So, for this, we will be using an open-source web-based Automation tool -Selenium. This Browser Automation in Python with Selenium allows you to make simple and efficient browser automation successfully.

In this tutorial, we will learn how you can open and close the browser, land on a website using a code, search in the search bar and take a screenshot using code in python.

Let’s have a look at how it works step by step.

 

Requirements

1. Python 3.9 interpreter and IDLE (online or system configured)

2. Selenium modules

3. Chrome Driver

Steps to Create Browser Automation In Python With Selenium

Step 1: Download Chrome Driver wrt your version of chrome browser, using the below link, and set its path.

https://chromedriver.chromium.org/downloads

Step 2: Install the latest version of python using the below link,

https://www.python.org/downloads/

Step 3: Install Selenium. Paste the below line and press enter.

Windows: Open Command Prompt and type

macOS: Open Terminal and type

pip3 install selenium

Step 4: Now, type py in your command prompt and execute step by step below script to perform various tasks using code in your browser.

Source Code

# Import Modules

from selenium import webdriver

from PIL import Image




# Here, we are assigning the path location of the chrome driver to a variable named as browser

browser = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")




# The below function maximises the current window that the web driver is using

browser.maximize_window()




# The below function loads a web page in the current browser session

browser.get("https://myprojectideas.com/")




# To find an element by name 

temp = browser.find_element_by_name("s")




# click function clicks the element

temp.click()




# send_keys simulates typing into the element

temp.send_keys("Python")




# To save screenshot of the current window to a PNG image file

browser.save_screenshot("demo.png")




# To view the downloaded image

image = Image.open("demo.png")

image.show()




# The close function closes the current window

browser.close()

Output

The below screenshot shows the resultant output of our browser automation in python using selenium through which we have operated our chrome browser.

Browser Automation in Python with Selenium

Explanation Of The Code

In the beginning, we imported modules for automation and image purpose. After it, we assign the chrome driver’s path location to a variable named browser. Now, different tasks can be performed using different functions mentioned below.

1. maximize_window() function maximises the web driver’s current window.

2. get() function loads a web page in the current browser session.

3. find_element_by_name() function helps in finding an element by its name. To find the name of the element, inspect the element is used.

4. Click function clicks the element.

5. send_keys() simulate typing into the element.

6. save_screenshot() saves a screenshot of the current window to a PNG image file.

7. To view the downloaded image, the show function is used.

8. At last, the close function closes the current window.

Things to Remember

  • Set the path of the driver after extracting its zip file.
  • Before using the script, import Selenium using a command prompt to avoid errors.
  • Python is a case sensitive language. Hence, don’t change the cases unnecessarily.

 

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published.