Element Handling in Selenium

Selenium Tutorial
Element Handling in Selenium

I) Web Elements and Operations on Web Elements
II) Elements Locators in Selenium
III) Elements Handling in Selenium
-------------------------------
I) Web Elements and Operations on Web Elements

i) Web Elements

Browser,
Page
-------------------
Edit Box,
Text Box,
Link,
Button,
Image, Image Link, Image Button
Text Area, Error Message
Check Box,
Radio Button,
Drop Down Box
List Box
Combo Box (Edit Box and Drop Down)
Web Table/HTML Table
Frame Etc...
-------------------------------
ii) Operations on Web Elements

1) Browser

> Launch Browser
> Navigate to particular URL
> Close Browser
> Close all Opened Browsers
-------------------------------
> Navigate From one url to another
> Navigate Back
> Navigate Forward
> Minimize the Browser
> Maximize the Browser
> Full Screen the Browser
> Refresh the Browser
> Capture the Browser URL
> Select Home Page
-------------------------------
2) Page

> Get Page Title
> Get Page Source
> Get Page URL
-------------------------------
3) Operations on Edit Box

> Check the Existence of Edit Box
> Check the Enabled Status
> Enter a Value
> Edit the Existing value
> Clear the Value
> Get / Return the Value
-------------------------------
4) Operations on Link

Text Link...
Image Link...

Text Link...
 Internal Link
 External Link
-------------------------------
> Check the Existence of the Link
> Check Enabled Status
> Click
> Return Link Name
-------------------------------
5) Image 

a) General Image - 
 Check the Existence of the Image
 Check the Enabled Status of the Image
 Return Image Name etc...

b) Image Link
 > Check the Existence of the Image Link
 > Check Enabled Status
 > Click
 > Return Image Link Name
c) Image Button
 > Check the Existence of the Image Button
 > Check the Enabled status of the Image Button
 > Click/Submit
 > Return Image Button Name....
-------------------------------
6) Operations on Check box

> Check the Existence of the Check box
> Check the Enabled of the Check box
> Check the Selected of the Check box

> Select
> Unselect
-------------------------------
7) Operations on Button

> Check the Existence of the button
> Check the Enabled Status
> Click
> Return Button Name...
-------------------------------
8) Operations on Radio Button

> Check the Existence of the Radio Button
> Check the Enabled Status of the Radio Button
> Check the selected Status of the Radio Button
> Select
-------------------------------
9) Operations on Drop down Box

> Check the Existence of the Drop down Box
> Check the Enabled Status of the Drop down Box
> Select an Item
> Return Items Count...
-------------------------------
10) Operations on List Box

> Check the Existence of the List Box
> Check the Enabled Status of the List Box
> Select one or more items
> Return Items Count...
-------------------------------
11) Operations on Combo Box

> Check the Existence of the Combo Box
> Check the Enabled Status of the Combo Box
> Select an Item..
> Return Items Count....
> Enter a Value...
-------------------------------
12) Operations on Web Table or HTML Table

> Check the Existence of the Web Table
> Return a Cell Value...
> Return Rows Count
> Return Column Count...
-------------------------------
13) Operations on Frame

> Swith from Top Window to a Frame
> Swith to default content from a Frame
-------------------------------
II) Elements Locators in Selenium

1) id 

Syntax:

driverObject.WebDriverCommand(By.ElementLocator/id("Locator value"));

Example:
driver.findElement(By.id("Email"));
driver.findElement(By.id("Email")).sendKeys("India123");
-------------------------------
driver - Browser Object
findElement - WebDriver Command - to identify the element based on given locator...
By - Predefined Class
id - Locator
Email - Locato value 
sendkeys - WebDriver Command - To perform operation on the Element
India123 - Input Data
-------------------------------
2nd Syntax:

WebElement elementName = driverObject.WebDriverCommand(By.ElementLocator("Locatot value"));

elementName.WebDriverCommand();

Example:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
WebElement ebox = driver.findElement(By.id("Email"));
ebox.sendKeys("VijayB");
Thread.sleep(5000);
ebox.clear();
Thread.sleep(5000);
ebox.sendKeys("BVijayBangalore");
-------------------------------
Note: Whenever we want perform multiple operatons on any Element then choose this 2nd Syntax...
-------------------------------
2) name

Syntax:
driverObject.WebDriverCommand(By.ElementLocator/name("Locatot value"));

Example:
driver.findElement(By.name("username")).sendKeys("Pavani123");
-------------------------------
2nd Sysntax:

WebElement Username = driver.findElement(By.name("username"));
Username.sendKeys("123Pavani");
-------------------------------
3) className

driver.get("http://www.google.com");
driver.findElement(By.className("gb_P")).click();
-------------------------------
4) tagName

driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/LoginPage.html");
driver.findElement(By.tagName("input")).sendKeys("Jyothi");
-------------------------------
5) linkText

Sytax:

driverObject.WebDriverCommand(By.ElementLocator/linkText("Locatot value"));

Example:
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Images")).click();
-------------------------------
6) partialLinkText

driver.get("https://www.google.co.in");
driver.findElement(By.partialLinkText("a")).click();
-------------------------------
7) cssSelector 

Sysntax:
driverObject.WebDriverCommand(By.ElementLocator/cssSelector("cssSelector Value"));

Example:
driver.get("https://www.google.co.in");
driver.findElement(By.cssSelector(".gb_P")).click();
-------------------------------
8) xpath

xpath is defined as XML path, It is a syntax for identifying any element on the web page using XML path expression.
xpath is used to find the location of any element on a web page using HTML DOM structure.

Syntax:
driverObject.WebDriverCommand(By.ElementLocator/xpath("xpath Value"));

Example:
driver.get("https://www.google.co.in");
driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
-------------------------------
III) Elements Handling in Selenium

1) Handling Browser

> Launch Browser (Create browser driver instance...)
> Navigate to specific URL
> Return Current URL
> Return Browser Title
> Return Page Source
> Return Window Handle
> Close Focused Browser
> Close all Browsers that opened by WebDriver during Execution.
-------------------------------
> Navigate to another URL
> Navigate back to previous URL
> Navigate Forward
> refresh the Browser
> Minimize the Browser
-------------------------------
Selenium Webdriver Steps:

Example:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
System.out.println(driver.getTitle());

driver.navigate().to("https://in.yahoo.com/");
System.out.println(driver.getTitle());

driver.navigate().back();
System.out.println(driver.getTitle());

driver.navigate().forward();;
System.out.println(driver.getTitle());

driver.close();
-------------------------------
2) Handling Edit Box

> Check Displayed Status
> Check Enabled Status
> Enter some Value
> Return the Value
> Clear the Value
------------------------------------------
Selenium WebDriver Steps:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");
boolean displayStatus = driver.findElement(By.id("Email")).isDisplayed();
System.out.println(displayStatus);//true

boolean enabledStatus = driver.findElement(By.id("Email")).isEnabled();
System.out.println(enabledStatus);//true

driver.findElement(By.id("Email")).sendKeys("Vijay321");

String Email = driver.findElement(By.id("Email")).getAttribute("value");
System.out.println(Email);

Thread.sleep(4000);

driver.findElement(By.id("Email")).clear();
//driver.close();
-------------------------------
Note: Whenever you want to perform multiple operations on the same element in a Test Script then create
WebElement and perform operations one by one...

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");
String EmailValue = "Pavani123@gmail.com";

WebElement email = driver.findElement(By.id("Email"));

boolean a = email.isDisplayed();
System.out.println(a);

a = email.isEnabled();
System.out.println(a);

email.sendKeys(EmailValue);

String emailAddress = email.getAttribute("value");
System.out.println(emailAddress);

Thread.sleep(5000);

email.clear();

//driver.close();
-------------------------------
3) Handling Text Area, Error Message, Popup window

a) Handling Text Area

Returning the Text....

Example:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");
String textArea = driver.findElement(By.tagName("h1")).getText();
System.out.println(textArea);

b) Error Message

driver.findElement(By.id("next")).click();
Thread.sleep(3000);
String error_Message = driver.findElement(By.id("errormsg_0_Email")).getText();
System.out.println(error_Message);

c) Popup window

public class Class1 {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");
String textArea = driver.findElement(By.tagName("h1")).getText();
System.out.println(textArea);

driver.findElement(By.id("next")).click();
Thread.sleep(3000);
String error_Message = driver.findElement(By.id("errormsg_0_Email")).getText();
System.out.println(error_Message);

driver.navigate().to("https://mail.rediff.com/cgi-bin/login.cgi");
driver.findElement(By.name("proceed")).click();

Alert popUp = driver.switchTo().alert(); // Switch the driver focus from the web page th Popup/Alert message
String errorMessage2 = popUp.getText();
popUp.accept();
driver.findElement(By.id("login1")).sendKeys("Ravindra123@rediffmail.com");
System.out.println(errorMessage2);

driver.close();
-------------------------------
4) Handling Button (Submits)

> Check the Displayed Status
> Check the Enabled Status

> Click

> Return Name of the Object
> Return Type of the Object
-------------------------------
Selenium WebDriver Steps:

public class Class1 {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");

boolean displayStatus = driver.findElement(By.id("next")).isDisplayed();
boolean enabledStatus = driver.findElement(By.id("next")).isEnabled();

String typeoftheElement = driver.findElement(By.id("next")).getAttribute("type");
String nameoftheElement = driver.findElement(By.id("next")).getAttribute("value");

System.out.println(displayStatus);//true
System.out.println(enabledStatus);//true
System.out.println(typeoftheElement);//Submit
System.out.println(nameoftheElement);//Next

driver.findElement(By.id("next")).click();
driver.close();
-------------------------------
Create Web Element and conduct multiple operations...

public class Class1 {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");
WebElement button = driver.findElement(By.id("next"));

boolean displayStatus = button.isDisplayed();
boolean enabledStatus = button.isEnabled();

String typeoftheElement = button.getAttribute("type");
String nameoftheElement = button.getAttribute("value");

System.out.println(displayStatus);//true
System.out.println(enabledStatus);//true
System.out.println(typeoftheElement);//Submit
System.out.println(nameoftheElement);//Next

button.click();
driver.close();
-------------------------------
With Differnt Locators....

//Button with id Locator...
driver.findElement(By.id("next")).click();

//Button with name Locator...
driver.findElement(By.name("signIn")).click();

//Button with cssSelector Locator...
driver.findElement(By.cssSelector("#next")).click();

//Button with xpath Locator...
driver.findElement(By.xpath(".//*[@id='next']")).click();

Note: linkText and partialLinkText locators are not applicable for this element
-------------------------------
5) Handle Link

Types of Links in the Web (UI design point of view...)
 i) Text Link (Ex: gmail link in Google home page)
 ii) Image Link (Ex: Selenium IDE (Image) Link in seleniumhq.org home page)

Note: Redirecting to another Location / another Page or another Application
-------------------------------
Types of Links in the Web (Functionality Point of view...)
 1) Internal Link (Ex: Software Testing link in wikipedia.com)
 * Redirects to another location or another page in the same Application
 2) External Link (Ex: github.com link in wikipedia.com)
 * Redirects to another page in the Another Application
-------------------------------
Broken Links...

Operations on Link

> Check Displayed Status
> Check Enabled Status

> Click 

> Return Link name....
-------------------------------
Selenium WebDriver Steps:

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement link = driver.findElement(By.linkText("Gmail"));

boolean displayedStatus = link.isDisplayed();
boolean enabledStatus = link.isEnabled();

String linkName = link.getText();

System.out.println(displayedStatus);//true
System.out.println(enabledStatus);//true
System.out.println(linkName);//Gmail

link.click();
-------------------------------
// Handling Link element using "linkText" locator...
driver.findElement(By.linkText("Gmail")).click();

// Handling Link element using "partialLinkText" locator...
driver.findElement(By.partialLinkText("Gma")).click();

// Handling Link element using "Class Name" locator...
driver.findElement(By.className("gb_P")).click();

// Handling Link element using "cssSelector" locator...
driver.findElement(By.cssSelector(".gb_P")).click();

// Handling Link element using "xpath" locator...
driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
-------------------------------
6) Handling Radio Button

Operating Radio Button

> Check Displayed Status
> Check Enabled Status
> Check Selected Status

> Select / Click 
-------------------------------
Selenium WebDriver Test script:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?osCsid=7u1anl9ha5gejotirul66opl33");
boolean a = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isDisplayed();
System.out.println(a);//true

a = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isEnabled();
System.out.println(a);//true

a = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isSelected();
System.out.println(a);//false

driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).click();

a = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isSelected();
System.out.println(a);//true
-------------------------------
//Create Web Element and perform multiple operations on the Element

public static void main(String[] args) {
boolean status;
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?osCsid=7u1anl9ha5gejotirul66opl33");
WebElement radioButton = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]"));

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

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

status = radioButton.isSelected();
System.out.println(status);//false

radioButton.click();

status = radioButton.isSelected();
System.out.println(status);//true

driver.close();
-------------------------------
7) Handling Drop Down Box

> Check Displayed Status
> Check Enabled Status

> Select an Item

> Return Items Count
-----------------------------------------------------------
Selenium WebDriver script

public static void main(String[] args) {
boolean status;
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?osCsid=7u1anl9ha5gejotirul66opl33");
System.out.println(driver.findElement(By.name("country")).isDisplayed());//true
System.out.println(driver.findElement(By.name("country")).isEnabled());//true

Select dropdown = new Select(driver.findElement(By.name("country")));

//dropdown.selectByIndex(1);
dropdown.selectByVisibleText("India");

List <WebElement> e = dropdown.getOptions();
System.out.println(e.size());

driver.close();
-------------------------------
8) Handling Check Box

> Check Displayed Status
> Check Enabled Status
> Check Selected Status

> Select
> Unselect
-------------------------------
Selenium WebDriver Script:

public static void main(String[] args) {
boolean status;
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/MultipleCheckbox.html");
boolean displayedStatus = driver.findElement(By.xpath("html/body/input[3]")).isDisplayed();
boolean enabledStatus = driver.findElement(By.xpath("html/body/input[3]")).isEnabled();
boolean selectedStatus = driver.findElement(By.xpath("html/body/input[3]")).isSelected();
System.out.println(displayedStatus);//true
System.out.println(enabledStatus);//true
System.out.println(selectedStatus);//false

driver.findElement(By.xpath("html/body/input[3]")).click();

boolean selectedStatus2 = driver.findElement(By.xpath("html/body/input[3]")).isSelected();
System.out.println(selectedStatus2);//true

driver.findElement(By.xpath("html/body/input[3]")).click();

boolean selectedStatus3 = driver.findElement(By.xpath("html/body/input[3]")).isSelected();
System.out.println(selectedStatus3);//false
-------------------------------
// Create Web Element and perform operations one by one

public static void main(String[] args) {
boolean status;
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/MultipleCheckbox.html");
WebElement checkBox = driver.findElement(By.xpath("html/body/input[3]"));

boolean displayedStatus= checkBox.isDisplayed();
boolean enabledStatus = checkBox.isEnabled();
boolean selectedStatus1= checkBox.isSelected();

System.out.println(displayedStatus);//true
System.out.println(enabledStatus);//true
System.out.println(selectedStatus1);//false

checkBox.click();

boolean selectedStatus2= checkBox.isSelected();
System.out.println(selectedStatus2);//true

checkBox.click();
boolean selectedStatus3= checkBox.isSelected();
System.out.println(selectedStatus3);//false
driver.close();
-------------------------------
8) Image, Image Button, Image Link

public static void main(String[] args) throws InterruptedException {
boolean status;
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co");
String imageTitle = driver.findElement(By.id("hplogo")).getAttribute("title");
System.out.println(imageTitle);

Thread.sleep(5000);

driver.navigate().to("http://newtours.demoaut.com/");
driver.findElement(By.name("login")).click();

Thread.sleep(5000);

driver.navigate().to("http://www.seleniumhq.org/");
driver.findElement(By.xpath(".//*[@id='choice']/tbody/tr/td[2]/center/a/img")).click();
-------------------------------
10) Handling Web Table/HTML Table

> Check Displayed Status

> Get/Return Cell Value
> Return Rows Count
> Return Cells Count

Selenium WebDriver Test Steps:
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/GCREDDY/Desktop/HTMLExamples/htmlTable.html");
boolean displayStatus = driver.findElement(By.id("students")).isDisplayed();
System.out.println(displayStatus);//true

WebElement studentsTable = driver.findElement(By.id("students"));

List <WebElement> rows = studentsTable.findElements(By.tagName("tr"));
int rowcount = rows.size();
System.out.println("Rows Count is: " +rowcount);

List <WebElement> cells = studentsTable.findElements(By.tagName("td"));
int cellCount = cells.size();
System.out.println("Cells Count is: " + cellCount);

System.out.println("Colums Count is: "+ cells.size()/rows.size());

String cellValue = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td[2]")).getText();
System.out.println(cellValue);

driver.close();
}
}
-------------------------------
Using Chrome browser:

//Instantiate the Chrome Browser driver
System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
//Create Chrome Browser driver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/GCREDDY/Desktop/HTMLExamples/htmlTable.html");
boolean displayStatus = driver.findElement(By.id("students")).isDisplayed();
System.out.println(displayStatus);//true

WebElement studentsTable = driver.findElement(By.id("students"));

List <WebElement> rows = studentsTable.findElements(By.tagName("tr"));
int rowcount = rows.size();
System.out.println("Rows Count is: " +rowcount);

List <WebElement> cells = studentsTable.findElements(By.tagName("td"));
int cellCount = cells.size();
System.out.println("Cells Count is: " + cellCount);

System.out.println("Colums Count is: "+ cells.size()/rows.size());

String cellValue = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td[2]")).getText();
System.out.println(cellValue);

driver.close();
}
}
-------------------------------
11) Handling Frames...

> HTML Frames are used to divide the Browser window into multiple sections, where each section
can load a separate HTML document

> Frames are Sections of Web page displed on Top window

> Whenever we access the page then focus on the top window.
-------------------------------
Note: If It is Manual Testing, we no need to focus on Frames, we can access Elements in any frame directly.

In Test Automation using Selenium Webdriver, first we need to switch to a frame then we can access
elements in that frame.
-------------------------------
Switch from Top window to a Frame is done in two ways,

i) Using Frame Index
ii) Using Frame Name
-------------------------------
i) Using Frame Index

Syntax:
driver.switchTo().frame(int index);

ii) Using Frame Name
driver.switchTo().frame("String Frame Name");
-------------------------------
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
//Switch from the Top window to 3rd Frame using Frame Index
driver.switchTo().frame(2);
driver.findElement(By.linkText("org.openqa.selenium")).click();
-------------------------------
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
//Switch from the Top window to 3rd Frame using Frame Name
driver.switchTo().frame("classFrame");
driver.findElement(By.linkText("org.openqa.selenium")).click();
-------------------------------
Test Requirement:

> Launch the Web Page that has multiple Frames,
> Operate an Element in the 3rd Frame,
> Operate an Element in the 1st Frame
-------------------------------
Steps for selenium WebDriver test Script:

> Launch the Web Page that has multiple Frames,
> Switch from top window to 3rd Frame
> Access an Element
> Back to Top window (default)
> Switch from Top window to 1st Frame
> Access an Element
-------------------------------
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
//Switch from the Top window to 3rd Frame using Frame Name
driver.switchTo().frame("classFrame");
driver.findElement(By.linkText("org.openqa.selenium")).click();

driver.switchTo().defaultContent();

driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("com.thoughtworks.selenium")).click();
-------------------------------
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
//Switch from the Top window to 3rd Frame using Frame Index
driver.switchTo().frame(2);
driver.findElement(By.linkText("org.openqa.selenium")).click();

driver.switchTo().defaultContent();

driver.switchTo().frame(0);
driver.findElement(By.linkText("com.thoughtworks.selenium")).click();
driver.close();
-------------------------------

1 comments:

awesome Sir. it's very useful to us.

Post a Comment