top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Basics of Selenium

What is Selenium?

- Selenium is automation tool which is used to automate web based application

for software automation process.

- It contains several classes, commands which are useful to handle different

web element.

Selenium Tool Suite

  • Selenium Integrated Development Environment (IDE)

  • Selenium Remote Control (RC)

  • WebDriver

  • Selenium Grid

Advantages of Selenium:

  • It is open source automation tool.

  • It supports multiples programming languages such as java, python, C sharp etc.

  • Cross browser testing is possible

  • Selenium supports Parallel Test Execution.

  • Selenium Test Case Execution time is faster than other tools like UFT, RFT, TestComplete, SilkTest, etc.

Disadvantages of Selenium:

  • We can’t automate desktop based application.

  • We can’t automate standalone applications.

  • We can’t automate captcha code using selenium.

  • We can’t read barcode using selenium tool.

  • It doesn’t support file uploading.

  • Limited support for Image Testing.

Different java concepts used in selenium:

  • Inheritance

  • Interface

  • Polymorphism

  • Casting (upcasting)

  • Encapsulation

  • Abstraction

  • Arrays

  • Collection , List, HashMap, HashSet

  • Loops

  • Control statements

  • String

Selenium Types:

1. Selenium IDE:

- IDE stands for Integrated Development Environment.

- In this version of selenium we can’t perform compatibility testing.

- We can run our script only in Mozilla Firefox browser.

- We have record and playback option in this IDE.

2. Selenium RC:

- This type of selenium can support compatibility testing.

- That means we can run test scripts in different browser such as chrome,

Firefox, internet explorer etc. but we can use only Java programming language to

write the script.

3. Selenium Webdriver:

-This type of selenium can support compatibility testing. That means we can run

test scripts in different browser such as chrome, Firefox, internet explorer etc.

- It can support multiple programming languages such as java, python C sharp

etc to write the script.

- Currently we are using selenium tool having version 4

4. Selenium grid

- It works with Selenium RC.

- Server is required for selenium grid to start automation

- Core engine is JavaScript base


Selenium Architecture:


1. Search Context: It is a super most interface in selenium. It consists of all abstract methods and that methods are inherited to the Webdriver interface.


2. Webdriver: It is an interface present in selenium which consists of two types of abstract methods that is abstract methods of search context and his own abstract methods.


3. Selenium Remote Webdriver: It a class which implements all the abstract methods of both the interfaces that is search context and Webdriver. This implementation class is extended to the different browsers such as chrome. Firefox, internet explorer etc.


4. Browser driver: For compatibility testing we need to use runtime polymorphism to perform up casting in selenium. For example, to open a browser using script, we create an object of chrome driver with reference of Webdriver interface.

WebDriver driver = new ChromeDriver ();


ree

ree


WebElement

The term WebElement refers to a HTML element. The HTML documents are

composed of HTML elements. It consists a start tag, an end tag and

the content in between.

WebElement commands

Before going through each and every action of WebElement, just understand

that how we get a WebElement object/element. we

learned that every method of the WebDriver either returns something or return

void(means return nothing). The same way find Element command

of WebDriver returns WebElement.

WebElement can be of any type, like it can be

a Text, Link, Radio Button, Drop Down, Web Table or any HTML element. But

all the actions will always populate against any element irrespective of whether

the action is valid on the WebElement or not. For e.g. clear() command, even if

you have a link element still you get the option to choose clear() command on it,

which if you choose may result in some error or may not does anything.

The findElement

The findElement(By, by) method searches and locates the first element on

the current page, which matches the criteria given as a parameter. This

method is usually used in commands to simulate user actions like click, submit,

type etc.


​findElement

findElements

Throws NoSuchElementException in case

there are no matching elements.

​Returns an empty list in case

there are no matching elements.

​Returns a single web element

​Returns a collection of web elements


DOM:

It is an API interface provided by the browser.(every browser has this api

internally).


DOM stands for Document Object Model. In simple words, DOM specifies the

structural representation of HTML elements.

When a web page is located, the browser automatically creates the DOM of the

page

All the html tags and hierarchy will be aligned in the form of DOM structure.


Locators:


What is Locator?

  • The locator can be termed as an address that identifies a web element uniquely within the web page.

  • Locators are the HTML properties of a web element which tells the Selenium about the web element

it needs to perform the action on.

  • Locator is a command that tells Selenium IDE/Selenium which GUI elements (say TextBox, Buttons,

Check Boxes etc) its needs to operate on.

  • Identification of correct GUI elements is a prerequisite to creating an automation script.


Locating By ID :

This is the most common way of locating elements since ID's are supposed to be unique for each

element.


Example: driver.findElement(By.id("UserName"));			

Locating By Name :

Locating elements by name are very similar to locating by ID, except that we use the "name=" prefix


        Example: driver.findElement(By.Name("UserName"));
		

Locating By ClassName :

Locating elements by classname as very rare as most of the elements on screen share the same class

name

  Example: driver.findElement(By.className("CS_HEADER"));   		


Locating By Link Text

This type of locator applies only to hyperlink texts. We access the link by prefixing our target with "link="

and then followed by the hyperlink text.


Example: driver.findElement(By.linkText("Login Page"));   

Locating By Partial Link Text

This type of locator applies only to hyperlink texts where partial link and text is given . We access the link by prefixing our target with "partial link =" and then followed by the hyper link text.


Example: driver.findElement(By.partialLinkText("Login"));  

XPath Locators -Types

  • XPath is used to locate a web element based on its XML path.

  • XML stands for Extensible Mark up Language and is used to store , organize and transport arbitrary data.

  • It stores data in a key - value pair which is very much similar to HTML tags.

Absolute XPath:-

  • It is a direct way to locate an element .

  • It is very brittle.

  • It is unidirectional.

  • If there is change in UI of application then absolute xpath will not work so it is not reliable.

  • Starts with single "/" that means starting to search from the root.


Example: /html/body/div[2]/div/div[2]/div[1]/div[2]/form/div/input

ree




package Locators;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class absolute_locator {


public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Sumod\\Desktop\\Automation jar\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://opensource-demo.orangehrmlive.com/");

Thread.sleep(2000);

driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div[2]/div[2]/form/div[2]/input")).sendKeys("Admin");

Thread.sleep(2000);

driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div[2]/div[2]/form/div[3]/input")).sendKeys("admin123");

Thread.sleep(2000);

driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div[2]/div[2]/form/div[5]/input")).click();

}

}






Relative XPath :

  • Starts from the middle of the HTML DOM.

  • Starts with a double "//" that means it can start to search anywhere in the DOM structure.

  • Shorter than Absolute XPath.

  • It is bidirectional.

  • Highly recommended to use as it is more reliable.

  • In relative xpath we can traverse from parent to child by skipping the intermediate lines also we can go back to parent grandparent etc as well

  • Syntax : //tagname[@attribute = 'value']


Example : //div[@class = 'form-group']//input[@id = 'user-message']

Sample Code using Relative Xpath


package Locators;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class xpath_using_attribute

{


public static void main(String[] args) throws InterruptedException

{

driver.findElement(BySystem.setProperty("webdriver.chrome.driver", "C:\\Users\\shree\\Desktop\\Automation jar\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://opensource-demo.orangehrmlive.com/");

Thread.sleep(2000);

// enter username

driver.findElement(By.xpath("//input[@id=\"txtUsername\"]")).sendKeys("Admin");

//enter password.xpath("//input[@type=\"password\"]")).sendKeys("admin123");

//click on login button

driver.findElement(By.xpath("//input[@value=\"LOGIN\"]")).click();

}


}


CSS Selector

Locator which can identify element. CSS Selector combines an element selector and a selector value that can identify particular elements on a web page. Like XPath, CSS selector can be used to locate web elements without ID, class, or Name.

Syntax:

Driver.findElement(By.cssSelector(“expression”));


Methods of cssSelector:

  • using id attribute:

Syntax:

Tagname#idattributevalue

  • using class attribute:

Synax:

Tagname.classnamevalue

  • using any attribute:

Syntax:

Tagname[attributename=”attributevalue”]

  • Hybrid:

Syntax:

Tagname#idattributevalue[attributename=”attributevalue”]

  • using substring:

a. startswith(^)

Syntax:

Tagname[attributename^=”startingvalue”]

b. endswith($)

Syntax:

Tagname[attributename$=”endvalue”]

c. contains(*)

Syntax:

Tagname[attributename*=”partial value”]




CSS Selector Locators

CSS selectors are string patterns used to identify an element based on a combination of HTML tag, id, class and attributes.

Locating by CSS selector is more complicated than the previous methods , but it is the most common locating strategy of advanced Selenium users because it can access even those elements that have no ID or name .

Some of the mainly used formats of CSS Selectors :

  • Tag and ID

  • Tag and Class

  • Tag and Attribute

  • Tag, Class and Attribute

  • Sub-String matches

> Starts With (^)

> Ends With ($)

> Contains(*)



1> Tag and ID

ID -Represented by #

Syntax : TagName#ValueOfID

Example : input#Email


2> Tag and Class

Class -Represented by

Syntax : TagName.ClassName

Example : input.BaseClass


3> Tag and Attribute

TagName

Syntax : TagName[attName = attValue]

Example : button[type = 'submit']


4> Tag, Class and Attribute

TagName

Syntax : tag.class[attName = attValue]

Example :button.class[type ='submit']



Xpath Locators -Methods


1> Contains() is a method used in XPath expression. It is used when the value of any attribute changes dynamially, for example , login information

Complete value of 'Type' is 'submit' but using only partial value 'sub' -> Syntax: Xpath = //input[contains(@type,'sub')]

Complete value of 'name' is 'btnLogin' but using only partial value 'btn' -> Syntax: Xpath = //input[contains(@name,'btn')]


Xpath using Contains() Sample Example : -


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class xpathusingcontains_example1

{


public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\Users\\shree\\Desktop\\Automation jar\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("C:\\Users\\shree\\Desktop\\HTMLcode\\demo.html");

Thread.sleep(1000);

//enter username

driver.findElement(By.xpath("//input[contains(@class,\"1\")]")).sendKeys("abhgj");

//enter password

driver.findElement(By.xpath("//input[contains(@type,\"pass\")]")).sendKeys("abgc");

//open facebook application

driver.findElement(By.xpath("//a[contains(text(),\"Face\")]")).click();

driver.quit();

}


}


2> Starts-with() function finds the element whose attribute value changes on refresh or any operation on the webpage.

For example :- Suppose the ID of particular element changes dynamically like Id ="message12" | Id = "message345" | Id ="message8769"


Syntax : Xpath = //label[starts-with(@id,'message')]


Xpath using starts-with Sample Example : -


package Locators;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class xpathusingstartswith_example1 {


public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\Users\\shree\\Desktop\\Automation jar\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.flipkart.com/");

Thread.sleep(2000);

driver.findElement(By.xpath("(//button[starts-with(@class,\"_2KpZ6l\")])[1]")).click();

Thread.sleep(2000);

driver.findElement(By.xpath("//div[starts-with(text(),\"Mob\")]")).click();

}

}


3> Text() function , we find the element with exact text match as shown below . Suppose, we find the element with text "UserID"


Syntax : Xpath = //td[text()="UserID"]



ree

WebElement:

  • It is an interface use to perform action on element present on webpage.

  • If we want to perform multiple actions on same element then we can find that element at once and store it in reference variable having data type as web element. It will remove every time finding of same element. We just use that reference variable and perform the desired action on it.

Some important web element methods are given below.


Sendkeys( ) :

This method is use to enter value in the input/text field

Syntax:

WebElement ele=location of element; ----identify the webelement ele.sendKeys(“value”); -------perform action on that webelement

Clear( ):

This method is use to clear value in the text field.

Syntax: Ele.clear();

Click( ):

Click method is use to click on buttons, links also use to select radio buttons &

checkboxes.

Syntax:

Ele.click();

getText:

This method is use to get text present in a webpage. Return type of getText function is String.

Syntax:

Ele.getText();

isEnabled():

This method is use to verify element is enabled or disabled. Return type of isEnabled function Boolean

If element is enabled then it returns true otherwise it returns false.

Syntax:

Ele.isEnabled();

isDisplayed():

This method is use to verify element is present or not. Return type of isDisplayed function is boolean.

If element is present then it returns true otherwise it returns false.

Syntax:

Ele.isDisplayed();

isSelected( );

This method is used to verify radio/checkbox is selected or not. Return type of isSelected function is boolean. If radio button is selected then returns True otherwise False

Syntax:

Ele.isSelected();
















+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page