Handling Elements in Selenium


Handling Elements in Selenium WebDriver

Create and Execute Tests using Selenium WebDriver
 

Pre-requisites to create and execute Tests using WebDriver:

1) Web Element Locators (To Identify Elements/Objects)

2) WebDriver Methods (To Perform Operations on Elements)

3) Java Programming Concepts (To Enhance Tests or Test Cases)
---------------------------------------------
1) Element Locators:
 

i) id

ii) name

iii) className

iv) tagName

v) linkText

vi) partialLinkText

vii) cssSelector

viii) xpath
----------------------------------------------------
2) WebDriver Methods:
a) Browser general Methods

i) get()
ii) getTitle()
iii) getCurrentUrl()
iv) getPageSource()
v) close()
vi) quit()
------------------------
b) Browser Navigation methods

i) navigate().To() Method:
ii) navigate().back() Method
iii) navigate().forward() Method
iv) navigate().refresh(); Method

driver.manage().window().maximize();
----------------------------------
c) Methods on Elements

i) findElement()
ii) findElements()
iii) sendKeys()
iv) Clear()
v) Click()
vi) isEnabled()
vii) isDisplayed()
viii) isSelected()
ix) getText()
x) selectByVisibleText()
xi) selectByIndex()
Etc...
------------------------------
d) Others
explicitlyWait()
Etc...
-------------------------------------------------------
3) Java Programming Concepts:
Data Types, Variables, Strings, Arrays, Operators, Conditional and Loop Statements,

Built-in and User defined methods, Exception Handling,

Encapsulation, Abstraction, Polymorphism and Inheritance. etc...
-------------------------------------------------------
Handling Elements in Selenium WebDriver
i) Handle Browser
// Open URL
a) driver.get("http:/google.com")

// Return Browser Title
b) String s = driver.getTitle()

// Return Current URL
c) String s = driver.getCurrentUrl()

// Close focused Browser
d) driver.close

// Close all Browsers that opened by WebDriver
e) driver.quit
------------------------------------------
ii) Handle Edit box / Text Box
//Enter Value
a) driver.findElement(By.id("Email")).sendKeys("gcreddy");

// Get/Return value      
b) String s = driver.findElement(By.id("Email")).getAttribute("value");

// Return Element type
c) String s = driver.findElement(By.id("Email")).getAttribute("type");

// Return Element name
d) String s = driver.findElement(By.id("Email")).getAttribute("name");

// Clear Value
e) driver.findElement(By.id("Email")).clear();

// Find length of the Value
f) String s = driver.findElement(By.id("Email")).getAttribute("value");
   int a = s.length();

// Check if the Object is Selected
g) boolean s = driver.findElement(By.id("Email")).isSelected();
   System.out.println(s); //false
------------------------------------------
iii) Handle Link Element
// Click Link
a) driver.findElement(By.linkText("Gmail")).click();

// Click Link
b) driver.findElement(By.partialLinkText("Gmai")).click();

// Return Link Text
c) driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[2]/a")).click();
   String s = driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[2]/a")).getText();
   System.out.println(s);
------------------------------------------
iv) Handle Button
// Click Button
a) driver.findElement(By.id("signIn")).click();

// Return Element type
b) String s = driver.findElement(By.id("signIn")).getAttribute("type");
   System.out.println(s); //submit

// Return Button value
c) String s = driver.findElement(By.id("signIn")).getAttribute("value");
   System.out.println(s); //sign in

// Check if the Button is displayed
d) boolean s = driver.findElement(By.id("signIn")).isDisplayed();
   System.out.println(s); //true

// Check if the Button is enabled
e) boolean s = driver.findElement(By.id("signIn")).isEnabled();
   System.out.println(s); //true
------------------------------------------
v) Handle Text Area / Message
// Return Text Area Value
a) String s = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/h1")).getText();
   System.out.println(s);

// Retrun Error Message
b) driver.findElement(By.id("signIn")).click();
   String s = driver.findElement(By.xpath(".//*[@id='errormsg_0_Email']")).getText();
   System.out.println(s);
------------------------------------------
vi) Handle Drop down box /List box
// Select Drop down / List box item by Value
a) Select Dropdown = new Select(driver.findElement(By.name("fromPort")));
   Dropdown.selectByValue("London");

// Select Drop down / List box item by Index
b) Select Dropdown = new Select(driver.findElement(By.name("fromPort")));
   Dropdown.selectByIndex(1);

// Return items Count
c) Select Dropdown = new Select(driver.findElement(By.name("fromPort")));
   List<WebElement> e = Dropdown.getOptions();
   int i = e.size();
   System.out.println(i);
------------------------------------------
vii) Handle Radio Button
// Check if the Radio button is Selected
boolean a = driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[2]/td[2]/b/font/input[2]")).isSelected();
System.out.println(a);//false

// Select Radio Button      
driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[2]/td[2]/b/font/input[2]")).click();

// Check if the Radio button is Selected
boolean b = driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[2]/td[2]/b/font/input[2]")).isSelected();
System.out.println(b);//true
------------------------------------------
viii) Handle Check Box
// Check Check box
driver.findElement(By.xpath("html/body/input[2]")).click();

// Verify if the Check box is Selected.
boolean a =driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println(a);//true

// Uncheck Check box      
driver.findElement(By.xpath("html/body/input[2]")).click();
boolean b =driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println(b);//false
--------------------------------------------------
ix) Handle Image
Image, Image Button and Image Link:

a) Image Button:
//Click Image Button

driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys("gcreddy");
driver.findElement(By.name("password")).sendKeys("Bannuchinnu73");
String s = driver.findElement(By.name("login")).getAttribute("type");
System.out.println(s);
driver.findElement(By.name("login")).click();

b) Image Link// Click Image Link
driver.get("http://www.seleniumhq.org/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='choice']/tbody/tr/td[2]/center/a/img")).click();

c) Image// Return Image title value.
driver.get("https://www.google.com");
String s = driver.findElement(By.xpath(".//*[@id='hplogo']")).getAttribute("title");
System.out.println(s);
-----------------------------------------------
x) Handle Web Table / html Table
driver.get("file:///D:/HTMLExamples/htmlTable.html");

// Retrun Table Tag name
String s1 = driver.findElement(By.id("students")).getTagName();

// Return specified cell value
String s2 = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td[2]")).getText();
      
System.out.println(s1);
System.out.println(s2);

// Return Table Rows count

WebElement htmltable=driver.findElement(By.xpath(".//*[@id='students']/tbody"));
List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
int i1 = rows.size();
System.out.println(i1);
--------------------------------------------
xi) Handle Frames
> Frames are sections of html page displayed on the top window.

> Whenever we access the page then focus is on the top window.

> Switch to a frame is done using two ways.

i) using frame index
Syntax:

driver.switchTo().frame(int index)Example:

driver.switchTo().frame(2);
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();
--------------------------------
ii) using frame name
Syntax:

driver.switchTo().frame(String name)

Example:

driver.switchTo().frame("classFrame");
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();
--------------------------------
Example:

WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");

//Swictch from Top window to ClassFrame.
driver.switchTo().frame("ClassFrame");
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();

//Switch from ClassFrame to Top window.
driver.switchTo().defaultContent();

Example:
WebDriver driver = new FirefoxDriver();

driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame(2);
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();

Thread.sleep(4000);

driver.switchTo().defaultContent();
driver.switchTo().frame("packageListFrame");
driver.findElement(By.xpath("html/body/div[2]/ul/li[2]/a")).click();
--------------------------------------------
Handle Multiple Browsers
WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/HTMLExamples/LoginPage.html");
String parent = driver.getWindowHandle();

driver.findElement(By.linkText("Sign In")).click();
Set<String> Blist = driver.getWindowHandles();
int a = Blist.size();
System.out.println(a);

for (String s1:Blist) {
    if (s1 != parent){
        driver.switchTo().window(s1);
        System.out.println(driver.getCurrentUrl());
    }
}
driver.switchTo().window(s);
System.out.println(driver.getCurrentUrl());
--------------------------------------------
Handling Duplicate Elements:
driver.get("http://www.infibeam.com/");
driver.findElement(By.linkText("LOGIN / REGISTER")).click();
driver.findElement(By.xpath(".//*[@id='new-account-btn']")).click();
List <WebElement> e =driver.findElements(By.xpath(".//*[@id='password']"));
int i = e.size();
System.out.println(i);
e.get(1).sendKeys("gcreddy1");
------------------------------------------------
Handle span
driver.get("http://www.google.com/");
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gb23']/span[1]")).click();
-------------------------------------------------

3 comments:

Sir, your script for Handling duplicate elements doesn't work. Please help out!

Post a Comment