Handling Web Elements in Selenium

Handling Web Elements in Selenium, Inspect Web/HTML Elements, Locating elements, performing actions on elements, and writing Selenium Test Steps.

Handling Web Elements in Selenium, handle browser, edit box, button, link, checkbox, radio button, image, dropdown box, text, popup window, web/HTML table, web frame, etc,

Handling Web Elements using Selenium

Pre-requisites to create Test cases in Selenium

1. Web/HTML Elements
2. Element Locators (To recognize/identify Elements)
3. WebDriver Methods (To perform operations on Elements)
4. Programming features (To enhance Test cases)

1. Web/HTML Elements

Browser,
Page,
Frame,

Link – click – It redirects
Edit box – Enter a value – It holds the value
Button – click – It submits
Dropdown box – select an item – It holds the item
List Box – Select one or more items – It holds one or more items
Combo box (Dropdown box + edit box) – select an item or type an item
Radio button – click – It selects an option
Checkbox – click – cheek or uncheck
Text – Information/display
Image (general image, image link, and image button)
TextArea – Enter text –
Web Table/HTML Table
etc,

Date picker
Mouse hover
Inline Elements
Etc,

2. Element Locators

id()
name()
className()
tagName()
linkText()
partialLinkText()
cssSelector()
xpath()

3. Selenium WebDriver Methods

1. get(arg/url)
2. getTitle()
3. getCurrentUrl()
4. manage()
5. navigate()
.to(arg/url)
.back()
.forward()
.refresh()

6. close()
7. quit()

8. findElement()
9. findElements()
10. sendKeys()
11. click()
12. clear()
13. getAttribute()
14. getText()
15. isDisplayed()
16. isEnabled()
17. isSelected()

4. Programming Concepts

Java Standard Edition or Core Java
Java Programming Environment setup
Java Keywords & Identifiers, Java Syntax, and Progam Structure
Writing comments
Modifiers
Data Types and variables
Operators
Control Flow
Numbers, Strings, and Characters
Data Structures
IO
Exception Handling
Object-Oriented Programming
Project, Package, Class, Object, Methods, Constructor, Interface, Inheritance, Polymorphism, Abstraction, and Encapsulation.


Actions on Web/HTML Elements

1. Handling Browser

Manual Actions:

• Launch a Browser
• Load/Open a web page
• Maximize the Browser window
• Browser Full screen
• Return the Browser Title
• Return the Current URL
• Navigate to another page
• Navigate back to the previous page
• Navigate Forward
• Refresh the Browser
• Close the focused Browser window
• Close all Browsers that opened by Selenium WebDriver

Selenium WebDriver Actions:

public class Sample {
static WebDriver driver;
//Launch Browser
public void launchBrowser() {
System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
driver = new ChromeDriver();
}
//Close Browser
public void closeBrowser() {
driver.close();
}
public static void main(String[] args) throws InterruptedException {
Sample obj = new Sample();

//Launch Browser
obj.launchBrowser();

//Load/Open a web page
driver.get(“https://www.google.com/”);

//Maximize the Browser window
driver.manage().window().maximize();

Thread.sleep(3000);

//Browser Full screen
driver.manage().window().fullscreen();
Thread.sleep(3000);
driver.manage().window().maximize();

// Return the Browser Title
String pageTitle = driver.getTitle();
System.out.println(pageTitle);

//Return the Current URL
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

//Navigate to another page
driver.navigate().to(“https://www.icicibank.com/”);
Thread.sleep(3000);

//Navigate back to the previous page
driver.navigate().back();
Thread.sleep(3000);

//Navigate Forward
driver.navigate().forward();
Thread.sleep(3000);

driver.get(“https://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(“ABCD1234”);
Thread.sleep(3000);

//Refresh the Browser
driver.navigate().refresh();
Thread.sleep(3000);

//Close the focused Browser window
obj.closeBrowser();
Thread.sleep(3000);

obj.launchBrowser();
driver.manage().window().maximize();
//Close all Browsers that opened by Selenium WebDriver
driver.get(“https://www.gcreddy.com/2020/03/software-testing-project.html”);
driver.findElement(By.partialLinkText(“Admin”)).click();
Thread.sleep(3000);
driver.quit();
}
}

2. Handling Edit Box

Manual Actions:

• Check the Displayed status
• Check the Enabled status
• Entering a Value/Text
• Check the type of input
• Return the Value
• Clear the value

Selenium WebDriver Actions:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);
boolean status = driver.findElement(By.name(“username”)).isDisplayed();
System.out.println(status);//true

status = driver.findElement(By.name(“username”)).isEnabled();
System.out.println(status);//true

String typeofInput = driver.findElement(By.name(“username”)).getAttribute(“type”);
System.out.println(typeofInput);//text

driver.findElement(By.name(“username”)).sendKeys(“ABCD123”);
Thread.sleep(2000);

String val = driver.findElement(By.name(“username”)).getAttribute(“value”);
System.out.println(val);//ABCD123

driver.findElement(By.name(“username”)).clear();
Thread.sleep(3000);

driver.close();
……………………………………………
Writing Selenium WebDriver Test Step

1st Syntax:
//Locating an Element and Performing an Action on the Element
driverObject.WebDriverMethod/findElement(By.ElementLocator(“Value”)).WebDriverMethod();

2nd Syntax:
//Locating an Element/Creating WebElement
WebElement elementName= driverObject.WebDriverMethod/findElement(By.ElementLocator(“Value”));

elementName.WebDriverMethod2();

elementName.WebDriverMethod2();

elementName.WebDriverMethod3();
……………………………………………
Using ‘WebElement’

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);
WebElement editBox= driver.findElement(By.name(“username”));

boolean status = editBox.isDisplayed();
System.out.println(“Displayed Staus is: “+status);//true

status = editBox.isEnabled();
System.out.println(“Enabled Staus is: “+status);//true

String typeofInput = editBox.getAttribute(“type”);
System.out.println(typeofInput);//text

editBox.sendKeys(“ABCD123”);
Thread.sleep(2000);

String val = editBox.getAttribute(“value”);
System.out.println(val);//ABCD123

editBox.clear();
Thread.sleep(3000);

driver.close();

3. Handling Links

Types of Links in the web:
a. Text Links (Ex: ‘Gmail’ link in Google home page)
b. Image Links (Ex: ‘TestBirds’ link in selenium.dev home page)
c. Internal Links
d. External Links
e. MailTo Links
f. Broken Links

Manual Actions:

• Check the Displayed status
• Check the Enabled status
• Click
• Return Link name

Selenium WebDriver Actions:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.google.com/”);

WebElement gmail = driver.findElement(By.linkText(“Gmail”));

boolean status = gmail.isDisplayed();
System.out.println(status);//true

status = gmail.isEnabled();
System.out.println(status);//true

String linkName = gmail.getText();
System.out.println(linkName);

gmail.click();
Thread.sleep(3000);

driver.close();

4. Handle Text, Error Message, and Popup Window

Example:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);
//Return a text from a web page
String text = driver.findElement(By.className(“smallText”)).getText();
System.out.println(text);

driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(1000);

//Return an Error Message
String errorMessage = driver.findElement(By.className(“messageStackError”)).getText();
System.out.println(errorMessage);

driver.navigate().to(“https://mail.rediff.com/cgi-bin/login.cgi”);
driver.findElement(By.name(“proceed”)).click();
Thread.sleep(1000);

Alert popUp = driver.switchTo().alert(); //Changing driver focus from the Browser window to popup widow
//Return the Error message from a PopUp window
String message = popUp.getText();
Thread.sleep(1000);
System.out.println(message);
popUp.accept();

Thread.sleep(3000);
driver.close();

5. Handling Check box

Manual Actions:

• Check Displayed status
• Check Enabled status
• Check Selected status
• Click (Check/Uncheck)

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/create_account.php”);

boolean status = driver.findElement(By.name(“newsletter”)).isDisplayed();
System.out.println(“Displayed status is: “+status);//true

status = driver.findElement(By.name(“newsletter”)).isEnabled();
System.out.println(“Enabled status is: “+status);//true

status = driver.findElement(By.name(“newsletter”)).isSelected();
System.out.println(“Intial Selected status is: “+status);//false

driver.findElement(By.name(“newsletter”)).click();

status = driver.findElement(By.name(“newsletter”)).isSelected();
System.out.println(“Selected status is: “+status);//true

driver.findElement(By.name(“newsletter”)).click();

status = driver.findElement(By.name(“newsletter”)).isSelected();
System.out.println(“Selected status is: “+status);//false

driver.close();
…………………………..
Web Element – Element or Object in Web Pages
WebElement – Selenium Concept
………………………….
Using ‘WebElement’

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/create_account.php”);

WebElement checkBox = driver.findElement(By.name(“newsletter”));

boolean status = checkBox.isDisplayed();
System.out.println(“Displayed status is: “+status);//true

status = checkBox.isEnabled();
System.out.println(“Enabled status is: “+status);//true

status = checkBox.isSelected();
System.out.println(“Intial Selected status is: “+status);//false

checkBox.click();

status = checkBox.isSelected();
System.out.println(“Selected status is: “+status);//true

checkBox.click();

status = checkBox.isSelected();
System.out.println(“Selected status is: “+status);//false

driver.close();

6. Handle Button

Manual Actions

• Check Displayed status
• Check Enabled status
• Click (Submits)
• Return the type of Element

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);

WebElement Login = driver.findElement(By.id(“tdb1”));

boolean status = Login.isDisplayed();
System.out.println(“Displaed Status is: “+status);//true

status = Login.isEnabled();
System.out.println(“Enabled Status is: “+status);//true

String elementType= Login.getAttribute(“type”);//submit
System.out.println(elementType);

Login.click();
Thread.sleep(3000);

driver.close();

7. Handle Radio Button

Manual Actions:

• Check Displayed status
• Check Enabled status
• Check Selected status
• Click (Select an Option)

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/create_account.php”);

WebElement radioButton = driver.findElement(By.xpath(“//*[@id=\”bodyContent\”]/form/div/div[2]/table/tbody/tr[1]/td[2]/input[2]”));

boolean status = radioButton.isDisplayed();
System.out.println(“Displayed Status is: “+status);//true

status = radioButton.isEnabled();
System.out.println(“Enabled Status is: “+status);//true

status = radioButton.isSelected();
System.out.println(“Initial Selected Status is: “+status);//false

radioButton.click();

status = radioButton.isSelected();
System.out.println(“Selected Status is: “+status);//true

driver.findElement(By.name(“gender”)).click();

status = radioButton.isSelected();
System.out.println(“Selected Status is: “+status);//false
driver.close();

8. Handle Dropdown box

Manual Actions:

• Check Displayed status
• Check Enabled status
• Select an Item
• Return Items Count

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/create_account.php”);

boolean status = driver.findElement(By.name(“country”)).isDisplayed();
System.out.println(“Displated Status is: “+status);//true

status = driver.findElement(By.name(“country”)).isEnabled();
System.out.println(“Enabled Status is: “+status);//true

Select dropDown = new Select (driver.findElement(By.name(“country”)));
//dropDown.selectByIndex(99);
dropDown.selectByVisibleText(“India”);

List myList = dropDown.getOptions();
int itemscount = myList.size();
System.out.println(itemscount);

Thread.sleep(4000);

driver.close();

Handling Web Elements in Selenium

9. Handling Frames

• HTML Frames are used to divide the Browser window into multiple sections
• Frames are sections of a web page diplayed on the Top window
……………………………..
• In Manual Testing we need not focus on Frames, we can handle any element in any frame directly.

• In Automated Testing or Test Automation using Selenium, first we need to switch to a frame from the Topwindow then we can operate element/s in that frame.
……………………………..
Example: Incorrect script

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.selenium.dev/selenium/docs/api/java/index.html?overview-summary.html”);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
……………………………..
Switch from Top window to a Frame is done in two ways

1. Using Frame Index
driver.switchTo.frame(int index);

2. Using Frame Name
driver.switchTo.frame(String name);
……………………………..
Using ‘Frame Index’

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.selenium.dev/selenium/docs/api/java/index.html?overview-summary.html”);
driver.switchTo().frame(2);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
……………………………..
Using ‘Frame Name’

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.selenium.dev/selenium/docs/api/java/index.html?overview-summary.html”);
driver.switchTo().frame(“classFrame”);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
……………………………..

Example2:

Test Steps:

• Launch a web page that has multiple frames
• Operate an Element in 3rd Frame
• Operate an Element in 1st Frame
• Close Browser Window

Manual Testing:

• Launch a Browser
• Load/Open a web page that has multiple frames
• Operate an Element in 3rd Frame
• Operate an Element in 1st Frame
• Close Browser Window

Automated Testing using Selenium:

• Launch a Browser
• Load/Open a web page that has multiple frames
• Switch from Top window to 3rd Frame
• Operate an Element in 3rd Frame
• Back to To window (default)
• Switch from Top window to 1st Frame
• Operate an Element in 1st Frame
• Close Browser Window

Selenium WebDriver Test Steps using Frame Index:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.selenium.dev/selenium/docs/api/java/index.html?overview-summary.html”);
driver.switchTo().frame(2);
Thread.sleep(3000);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
Thread.sleep(3000);

driver.switchTo().defaultContent();

driver.switchTo().frame(0);
driver.findElement(By.linkText(“org.openqa.selenium.chrome”)).click();
Thread.sleep(3000);

driver.close();

Selenium WebDriver Test Steps using Frame Name:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.selenium.dev/selenium/docs/api/java/index.html?overview-summary.html”);
driver.switchTo().frame(“classFrame”);
Thread.sleep(3000);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
Thread.sleep(3000);

driver.switchTo().defaultContent();

driver.switchTo().frame(“packageListFrame”);
driver.findElement(By.linkText(“org.openqa.selenium.chrome”)).click();
Thread.sleep(3000);

driver.close();

10. Handle Image, Image Link

//Handle Image
driver.get(“https://www.google.com/”);

//Return the ‘Google Image’ Title
String title = driver.findElement(By.id(“hplogo”)).getAttribute(“title”);
System.out.println(title);

//Handling Image Link
• Check Displayed Status
• Check Enabled Status
• Click

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.selenium.dev/”);
WebElement testBirds = driver.findElement(By.xpath(“/html/body/div[3]/div/div[1]/a/img”));

System.out.println(testBirds.isDisplayed());//true

System.out.println(testBirds.isEnabled());//true

testBirds.click();
Thread.sleep(3000);
driver.close();

11. Handling Web Table/HTML Table

• Check Displayed status
• Return a Cell value
• Return rows count
• Return cells count
• Return columns count

Selenium WebDriver Test Steps:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“file:///C:/Users/Hello/Desktop/Customer.html”);

boolean status = driver.findElement(By.xpath(“/html/body/table”)).isDisplayed();
System.out.println(status);//true

WebElement customerTable = driver.findElement(By.xpath(“/html/body/table”));

List rows = customerTable.findElements(By.tagName(“tr”));
int row_Count=rows.size();
System.out.println(row_Count);//4

List cells = customerTable.findElements(By.tagName(“td”));
int cell_Count = cells.size();
System.out.println(cell_Count);//12

int column_Count = cell_Count/row_Count;

System.out.println(column_Count);//3

String cellValue = driver.findElement(By.xpath(“/html/body/table/tbody/tr[2]/td[2]”)).getText();
System.out.println(cellValue);//Smith

driver.close();

Manual Testing Tutorial
Java Tutorial
Python Tutorial
SQL Tutorial
Selenium, Java, and Testing Videos
Follow me on social media:

2 thoughts on “Handling Web Elements in Selenium”

Leave a Comment