What is Selenium?
Selenium is a widely used open-source tool primarily used for automating web browsers. It provides a set of tools and libraries to interact with web browsers in order to automate tasks like testing web applications, scraping web content, or automating repetitive tasks in a browser environment.
Selenium supports multiple programming languages including Python, Java, C#, Ruby, and JavaScript. It allows users to write scripts to control web browsers programmatically, enabling actions such as clicking buttons, filling forms, navigating through web pages, and extracting data from web pages.
Selenium is commonly used in software testing for automating web application testing across different browsers and platforms. It helps ensure the functionality and compatibility of web applications by allowing automated testing in various environments. Additionally, Selenium is also used for web scraping, where it can extract data from websites for various purposes such as data analysis, research, or content aggregation. Learn more from selenium official website
Selenium WebDriver is the primary component of the Selenium suite, designed to automate interactions with web browsers. It provides a programming interface for controlling web browsers, enabling users to automate various browser-based tasks, such as testing web applications, scraping web content, or automating repetitive tasks.
WebDriver uses different ways to find and interact with elements on a web page. These ways are called locators.
In Selenium there is 8 locator that we can use to identify web element:
Those are:
- ID: It looks for elements with a unique ID attribute. By.id(” “)
- Name: It finds elements based on their name attribute. By.name(” “)
- Class Name: It finds elements based on their CSS class. By.className(” “)
- Tag Name: It finds elements based on their HTML tag name. By.tagName(” “)
- Link Text: It finds links by their exact visible text. By.linkText(” “)
- Partial Link Text: It finds links by part of their visible text. By.partialLinkText(” “)
- XPath: It uses XPath expressions to locate elements based on their position and attributes in the HTML. By.xpath()
- CSS Selector: It uses CSS selectors to locate elements based on their styles and attributes. By.cssSelector(” “)
Prerequisites for Selenium:
Step 1: Install Java Development Kit (JDK)
Ensure that you have the Java Development Kit (JDK) installed on your system. You can download it from the official Oracle website or use a package manager if you’re on a Unix-based system.
Step 2: Create Maven Project
Before we create a Maven project, please create a Folder in your desktop and name it Automation.
Using IntelliJ IDEA:
- Open IntelliJ IDEA: Launch IntelliJ IDEA IDE on your system.
- Create New Project: Go to
File
>New
>Project...
. save the project under Automation folder in your desktop. - Choose Maven: In the New Project dialog, select
Maven
from the left-hand menu. - Select Maven Archetype: Choose
maven-archetype-quickstart
or any other archetype you prefer, then clickNext
. - Configure Project: Enter the GroupId, ArtifactId, and other required details for your Maven project. Click
Next
. - Project Name and Location: Choose a name and location for your project, then click
Finish
. - Add Selenium Dependency: Open the
pom.xml
file in your project and add the Selenium dependency under the<dependencies>
section as shown in the previous instructions. - Write Your Selenium Tests: Create a new Java class in the
src/test/java
directory and start writing your Selenium tests. - Run Your Tests: You can run your Selenium tests directly from IntelliJ IDEA by right-clicking on your test class and selecting
Run
.
Using Eclipse:
- Open Eclipse IDE: Launch Eclipse IDE on your system.
- Create New Maven Project: Go to
File
>New
>Project...
. save the project under Automation folder in your desktop. - Select Maven Project: In the New Project wizard, expand the
Maven
folder, then selectMaven Project
. ClickNext
. - Configure Project: Ensure that the option
Create a simple project (skip archetype selection)
is unchecked. ClickNext
. - Choose Archetype: Select an archetype, such as
maven-archetype-quickstart
, then clickNext
. - Configure Project: Enter the GroupId, ArtifactId, and other details for your Maven project. Click
Finish
. - Add Selenium Dependency: Open the
pom.xml
file in your project and add the Selenium dependency under the<dependencies>
section as shown in the previous instructions. - Write Your Selenium Tests: Create a new Java class in the
src/test/java
directory and start writing your Selenium tests. - Run Your Tests: You can run your Selenium tests in Eclipse by right-clicking on your test class and selecting
Run As
>Java Application
.
Selenium Command :
| Setup WebDriver | Click on element | Write on element | Select from dropdown | getText from element | getAttribute Value from element | Select checkbox | Scroll down | Hover over on element | Right click on element | double click on element | Alert popup handle | Multiple window handle | iFrame handle |