Want your browser to perform all its actions on its own? Read This!

Selenium WebDriver- A tool to automate your Browser.

Hey Everyone! Thanks for tuning in! Before we dive deep into this amazing tool, appreciate yourself for your curiosity and your eagerness to learn something new today!

appreciate.gif

Let's start without any further adieu!

What is a Selenium Webdriver?

Selenium WebDriver is a web framework that allows you to execute cross-browser tests. It is a powerful tool for controlling a web browser through code. Keep up with me, and you'll realise how simple this is!

What we will achieve?

If you follow along, by the end of this tutorial, you will have written your first automation script using Selenium. For job seekers, this is useful in QA automated Testing, and for technology enthusiasts, it has various applications like an SMS bomber.

bomb.gif

Prerequisites

You must have a basic understanding of Python and a decent understanding of HTML.

Getting Started

We will be going forward with Python, though Selenium supports Java, Ruby, C#, Javascript and Kotlin as well. Installation of Selenium libraries for Python can be done using pip:

pip install selenium

Note :

pip is the package installer for python. Systems having the latest version of python (python3) have pip3 installed. In such a case, install Selenium using:

pip3 install selenium

If you don't have pip installed in your system, you can follow these steps.

I recommend working with pip3. To Install and learn more about pip3, follow these steps

Install Browser Drivers

This is just setting up your system to allow a browser to be automated/controlled through code. Through WebDriver, Selenium supports all major browsers on the market such as Chrome/Chromium, Firefox, Internet Explorer, Edge, and Safari. Where possible, WebDriver drives the browser using the browser’s built-in support for automation.

Download the relevant files for your respective browser using the links given below:

Agenda

Since we are now done with basic installations and setting up our systems ready to perform automation, I'll tell you what we'll achieve by the end of this tutorial. We'll write code that will make an automated browser go to %[youtube.com/], navigate to the search box, search for "machine learning" and press enter, without the use of our keyboard and mouse or any human intervention whatsoever.

IDE :

I'll suggest working with a Jupyter notebook. (Check out installation here: Link ).You can use pycharm, atom, sublime, VSC, or any other editors and IDEs of your choice.

Code:

Import Selenium and Webdriver libraries :

# Import Selenium and Webdriver libraries
import selenium
from selenium import webdriver

We start by importing the necessary Selenium libraries. The selenium library is imported, and we specifically import the webdriver module from Selenium.

Connecting the driver with code:

# Connecting the driver with code
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
  • In this segment, we connect the Selenium WebDriver with the Chrome browser. Here's a breakdown of the steps:

    1. We import the ChromeService class and ChromeDriverManager from Selenium libraries.

    2. We create an instance of ChromeService and provide the path to the ChromeDriver executable using ChromeDriverManager().install().

    3. We create a webdriver.Chrome instance, which represents the Chrome browser controlled by Selenium.

We will now go to our specified URL using driver.get(url) method like so:

    driver.get("https://www.youtube.com/")
  • Note: Beyond this setup, the webdriver.Chrome instance offers various methods and attributes to interact with the browser. Some of these include:

    • driver.get(url): Navigate to a URL.

    • driver.title: Get the title of the current page.

    • driver.current_url: Get the current URL.

    • driver.implicitly_wait(seconds): Set an implicit wait time for element interactions.

    • driver.find_element(by, value): Find a single web element.

    • driver.find_elements(by, value): Find multiple web elements.

    • driver.click(): Click on an element.

    • driver.send_keys(text): Simulate keyboard input.

    • driver.clear(): Clear input fields.

    • driver.back(): Navigate back in history.

    • driver.forward(): Navigate forward in history.

    • driver.refresh(): Refresh the current page.

    • driver.switch_to.frame(frame_reference): Switch to an iframe.

    • driver.execute_script(script, *args): Execute JavaScript on the page.

    • driver.maximize_window(): Maximize the browser window.

    • driver.minimize_window(): Minimize the browser window.

This list represents just a fraction of the functions and attributes available with the driver object in Selenium WebDriver. The extensive capabilities of Selenium allow you to automate complex interactions and testing scenarios on web applications.

If you're looking for more advanced usage or specific tasks, the official Selenium documentation provides comprehensive information on all available methods and classes.

Searchbox ,passing keys and pressing the search button:

from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
search_box = driver.find_element(by=By.NAME, value="search_query").send_keys("machine learning" + Keys.ENTER)
search_button=driver.find_element(by=By.CLASS_NAME, value= "style-scope ytd-searchbox")
  1. from selenium.webdriver.common.by import By

    • This line imports the By class from the Selenium library. By is used to specify the mechanism for locating web elements.
  2. from selenium.webdriver.common.keys import Keys

    • This line imports the Keys class from Selenium, which provides keyboard keys to simulate keyboard input.
  3. search_box = driver.find_element(by=By.NAME, value="search_query").send_keys("machine learning" + Keys.ENTER)

    • Here, we use Selenium's driver.find_element method to locate the search input element on the webpage. We specify that we want to locate it by its NAME attribute with the value "search_query." Then, we use send_keys to simulate typing "machine learning" and pressing Enter.

    • This code interacts with the search box on the YouTube website, typing "machine learning" in the input textfield.

  4. search_button = driver.find_element(by=By.CLASS_NAME, value="style-scope ytd-searchbox")

    • This line locates the search button element on the webpage. We use the CLASS_NAME attribute with the value "style-scope ytd-searchbox" to find the button element.

    • This button is now being clicked programmatically to initiate the search action.

How did we obtain the NAME and CLASS_NAME?

The obvious question that you might have, how did we get these NAME and CLASS_NAME for the search box and search button. For those super familiar with HTML can simply see the page source and try figuring this out. But there's a easier approach.

  • To find the NAME and CLASS_NAME of elements, you can use the "Inspect" or "Inspect Element" functionality in web browsers like Google Chrome. Here's how:

    1. Right-click on the web element you want to inspect (in this case, the search box or button).

    2. Select "Inspect" or "Inspect Element" from the context menu.

    3. The browser's developer tools will open, and you can see the HTML code of the selected element.

    4. Look for attributes like name or class in the HTML code to identify the element's NAME and CLASS_NAME.

Other Methods and Use Cases:

  • Selenium offers various methods and use cases for web scraping and automation:

    • element.click(): Click on buttons, links, or elements.

    • element.text: Retrieve text content from elements (e.g., reading article titles).

    • element.get_attribute("attribute_name"): Get attributes like href, src, or class from elements.

    • element.is_displayed(): Check if an element is visible.

    • element.is_enabled(): Check if an element is interactable.

    • element.send_keys(): Input data into forms or search boxes.

    • driver.get(url): Navigate to different web pages.

    • driver.navigate_back() and driver.navigate_forward(): Navigate backward and forward in the browser's history.

    • driver.refresh(): Refresh the current page.

This was just a beginning project that you build using selenium. Once, you get hold of this tool, you'll love it to heart, and applications of Selenium are infinite.

Meet you with some other interesting tool!

Till then,

Signing off,

With ❤️,

Dhruv Varshney