Python and Selenium automation

Python and Selenium automation

Introduction

With this simple blog post, I want to share with you what I have learned studying selenium and automating action.

  • What is selenium

  • How to automate stuff with selenium

What is selenium

Selenium is an open-source automation testing tool that is widely used for web application testing. It provides a set of libraries and APIs that allow developers to automate web browsers' interactions with web applications. Selenium can be used with a range of programming languages such as Java, Python, and C#. With Selenium, you can simulate a user's interactions with a web application, including clicking buttons, filling out forms, and navigating through pages. Selenium is particularly useful for testing web applications because it can test across different browsers and operating systems. Additionally, Selenium can be used for other web-related tasks such as web scraping and automating repetitive tasks. Overall, Selenium is an essential tool for web developers and testers who want to ensure their web applications are functioning correctly across various environments.

How to automate stuff with selenium

To get started with Selenium in Python, the first step is to install the Selenium library using pip, a package installer for Python.

pip install selenium
pip install webdriver_manager

Once installed, you can use Selenium's webdriver to launch a web browser and start interacting with web pages.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep


#initializing webdriver.
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

#navigating to google home page
driver.get("https://paoloferrari.hashnode.dev/")

#waiting some times
sleep(5)

#close the driver when all the action has been performed
driver.close()

With webdriver, you can perform various actions on a web page, such as finding elements by ID, class, or CSS selector, clicking buttons, filling in forms, and navigating through pages.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from time import sleep

#initializing webdriver.
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

#navigating to google home page
driver.get("https://paoloferrari.hashnode.dev/")

#wait some times
sleep(5)
#click on a blog post
driver.find_element(By.XPATH,"//*[@id='__next']/div/div/div/div/div[1]/section/div[1]/h1/a").click()

#waiting some times
sleep(5)
#click on the search bar
driver.find_element(By.XPATH,"//*[@id='__next']/div/header/div/div[1]/div[2]/button[1]").click()

#wait some times
sleep(5)
#write the title of a blog post
driver.find_element(By.XPATH,"//*[@id='__next']/div/header/div/div[4]/div[2]/div[1]/input").send_keys("project ideas")

#wait some times
sleep(5)
#click on the blog post in the search bar
driver.find_element(By.XPATH,"//*[@id='__next']/div/header/div/div[4]/div[2]/div[2]/a").click()

#wait some times
sleep(5)

#close the driver when all the action has been performed
driver.close()

As you can see in the small script that I have written, in selenium( based on my experience), you have to make sure that before performing every action, the element that you want to click or write in ecc., has been loaded otherwise you will get into some errors.

If you struggle to get the CSS selector or xpath of an element check out my post about it.

In conclusion, Python and Selenium are a powerful combination for automating web browser interactions and testing web applications. With its easy-to-use libraries and intuitive methods, Selenium makes it easy for developers to write automation scripts that can save them time and ensure their web applications are functioning correctly

Conclusion

I hope that with this simple tutorial about selenium, you could understand a little bit about how it works and how to use it. In the next few days I will publish a simple programming project that involves selenium, so if you are interested in what I do please consider following me.

Follow and support me:

Special thanks if you subscribe to my channel :)

Did you find this article valuable?

Support Paolo Ferrari by becoming a sponsor. Any amount is appreciated!