Handling Elements inwards Selenium Part-2
i) Handle Browser
ii) Handle Edit box
iii) Handle Text Area, Error Message, Window Dialog
iv) grip Button
-----------------------
Return/Capture value from Edit box
public static void main(String[] args) {
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("India123");
System.out.println(driver.findElement(By.id("Email")).getAttribute("value"));
}
}
-----------------------
v) Handle Image
Three types of Image elements inwards Web Environment.
1) General Image (No functionality)
2) Image Button (Submits)
3) Image Link (Directs to about other page/location)
Example:
public static void main(String[] args) {
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
System.out.println(driver.findElement(By.id("hplogo")).isDisplayed());//true
System.out.println(driver.findElement(By.id("hplogo")).getAttribute("title"));
driver.navigate().to("http://newtours.demoaut.com/");
driver.findElement(By.name("login")).click();
driver.navigate().to("http://www.seleniumhq.org/");
driver.findElement(By.xpath(".//*[@id='choice']/tbody/tr/td
[2]/center/a/img")).click();
}
}
-----------------------
vi) Handle Link
Operations on Link
> Click
> Check the Link Existence
> Check Enabled status
> Return Link Name etc...
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
//driver.findElement(By.className("gb_P")).click();
//driver.findElement(By.linkText("Gmail")).click();
//driver.findElement(By.partialLinkText("mail")).click();
//driver.findElement(By.cssSelector(".gb_P")).click();
//driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div
[1]/a")).click();
WebElement Gmail_Link = driver.findElement(By.xpath(".//*
[@id='gbw']/div/div/div[1]/div[1]/a"));
boolean linkStatus = Gmail_Link.isDisplayed();
System.out.println(linkStatus);//true
linkStatus = Gmail_Link.isEnabled();
System.out.println(linkStatus);//true
String LinkName = Gmail_Link.getText();
System.out.println(LinkName);
Gmail_Link.click();
}
}
-----------------------
vii) Handle Radio Button
Operations on Radio Button
> Select
> Check Enabled status
> Check Displayed status
> Check Selected status
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?
osCsid=47gtsrhe41613u5r3eqhgdbas7");
WebElement maleRadioButton = driver.findElement(By.xpath(".//*
[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]"));
boolean elementStatus = maleRadioButton.isDisplayed();
System.out.println(elementStatus);//true
System.out.println(maleRadioButton.isEnabled());//true
System.out.println(maleRadioButton.isSelected());//false
maleRadioButton.click();
System.out.println(maleRadioButton.isSelected());//true
}
}
-----------------------
viii) Handle Drop Down box
Operations on Drop Down Box
> Select an Item
> Check Displayed status
> Check Enabled status
> Items count
Example:
public static void main(String[] args) {
WebDriver driver = novel FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?
osCsid=47gtsrhe41613u5r3eqhgdbas7");
Select dropDown = novel Select (driver.findElement(By.name("country")));
//dropDown.selectByIndex(6);//Select an exceptional past times index
//dropDown.selectByVisibleText("India");
List<WebElement> e = dropDown.getOptions();
int itemsCount = e.size();
System.out.println(itemsCount);
}
}
-----------------------
ix) Handle Check box
Operations on Check box
> Select
> Unselect
> Check Displayed status
> Check Enabled status
> Check selected status
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("file:///E:/HTMLExamples/MultipleCheckbox.html");
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isDisplayed());//true
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isEnabled());//true
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isSelected());//false
driver.findElement(By.xpath("html/body/input[2]")).click();
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isSelected());//true
driver.findElement(By.xpath("html/body/input[2]")).click();
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isSelected());//false
-----------------------
x) Handle Web Table / HTML Table
Operations on Web Table:
> Get jail mobile telephone value
> Rows Count
> Cells Count
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("file:///E:/HTMLExamples/htmlTable.html");
String sec = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td
[2]")).getText();
System.out.println(s);
WebElement htmlTable = driver.findElement(By.id("students"));
List <WebElement> rows = htmlTable.findElements(By.tagName("tr"));
int r = rows.size();
System.out.println(r);
List <WebElement> cells = htmlTable.findElements(By.tagName("td"));
int c = cells.size();
System.out.println(c);
-----------------------
xi) Handle inline Elements
The bridge tag is used to grouping inline Elements inwards a Document.
Example 1:
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gb36']/span[1]")).click();
driver.navigate().back();
-----------------------
Example 2:
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gbwa']/div[2]/a[1]")).click();
driver.findElement(By.xpath(".//*[@id='gb300']/span[1]")).click();
-----------------------
Handle Frames
Handle Mouse Over
Working alongside Multiple browser windows.
-----------------------
i) Handle Browser
ii) Handle Edit box
iii) Handle Text Area, Error Message, Window Dialog
iv) grip Button
-----------------------
Return/Capture value from Edit box
public static void main(String[] args) {
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("India123");
System.out.println(driver.findElement(By.id("Email")).getAttribute("value"));
}
}
-----------------------
v) Handle Image
Three types of Image elements inwards Web Environment.
1) General Image (No functionality)
2) Image Button (Submits)
3) Image Link (Directs to about other page/location)
Example:
public static void main(String[] args) {
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
System.out.println(driver.findElement(By.id("hplogo")).isDisplayed());//true
System.out.println(driver.findElement(By.id("hplogo")).getAttribute("title"));
driver.navigate().to("http://newtours.demoaut.com/");
driver.findElement(By.name("login")).click();
driver.navigate().to("http://www.seleniumhq.org/");
driver.findElement(By.xpath(".//*[@id='choice']/tbody/tr/td
[2]/center/a/img")).click();
}
}
-----------------------
vi) Handle Link
Operations on Link
> Click
> Check the Link Existence
> Check Enabled status
> Return Link Name etc...
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
//driver.findElement(By.className("gb_P")).click();
//driver.findElement(By.linkText("Gmail")).click();
//driver.findElement(By.partialLinkText("mail")).click();
//driver.findElement(By.cssSelector(".gb_P")).click();
//driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div
[1]/a")).click();
WebElement Gmail_Link = driver.findElement(By.xpath(".//*
[@id='gbw']/div/div/div[1]/div[1]/a"));
boolean linkStatus = Gmail_Link.isDisplayed();
System.out.println(linkStatus);//true
linkStatus = Gmail_Link.isEnabled();
System.out.println(linkStatus);//true
String LinkName = Gmail_Link.getText();
System.out.println(LinkName);
Gmail_Link.click();
}
}
-----------------------
vii) Handle Radio Button
Operations on Radio Button
> Select
> Check Enabled status
> Check Displayed status
> Check Selected status
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?
osCsid=47gtsrhe41613u5r3eqhgdbas7");
WebElement maleRadioButton = driver.findElement(By.xpath(".//*
[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]"));
boolean elementStatus = maleRadioButton.isDisplayed();
System.out.println(elementStatus);//true
System.out.println(maleRadioButton.isEnabled());//true
System.out.println(maleRadioButton.isSelected());//false
maleRadioButton.click();
System.out.println(maleRadioButton.isSelected());//true
}
}
-----------------------
viii) Handle Drop Down box
Operations on Drop Down Box
> Select an Item
> Check Displayed status
> Check Enabled status
> Items count
Example:
public static void main(String[] args) {
WebDriver driver = novel FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?
osCsid=47gtsrhe41613u5r3eqhgdbas7");
Select dropDown = novel Select (driver.findElement(By.name("country")));
//dropDown.selectByIndex(6);//Select an exceptional past times index
//dropDown.selectByVisibleText("India");
List<WebElement> e = dropDown.getOptions();
int itemsCount = e.size();
System.out.println(itemsCount);
}
}
-----------------------
ix) Handle Check box
Operations on Check box
> Select
> Unselect
> Check Displayed status
> Check Enabled status
> Check selected status
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("file:///E:/HTMLExamples/MultipleCheckbox.html");
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isDisplayed());//true
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isEnabled());//true
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isSelected());//false
driver.findElement(By.xpath("html/body/input[2]")).click();
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isSelected());//true
driver.findElement(By.xpath("html/body/input[2]")).click();
System.out.println(driver.findElement(By.xpath("html/body/input
[2]")).isSelected());//false
-----------------------
x) Handle Web Table / HTML Table
Operations on Web Table:
> Get jail mobile telephone value
> Rows Count
> Cells Count
Example:
WebDriver driver = novel FirefoxDriver();
driver.get("file:///E:/HTMLExamples/htmlTable.html");
String sec = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td
[2]")).getText();
System.out.println(s);
WebElement htmlTable = driver.findElement(By.id("students"));
List <WebElement> rows = htmlTable.findElements(By.tagName("tr"));
int r = rows.size();
System.out.println(r);
List <WebElement> cells = htmlTable.findElements(By.tagName("td"));
int c = cells.size();
System.out.println(c);
-----------------------
xi) Handle inline Elements
The bridge tag is used to grouping inline Elements inwards a Document.
Example 1:
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gb36']/span[1]")).click();
driver.navigate().back();
-----------------------
Example 2:
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gbwa']/div[2]/a[1]")).click();
driver.findElement(By.xpath(".//*[@id='gb300']/span[1]")).click();
-----------------------
Handle Frames
Handle Mouse Over
Working alongside Multiple browser windows.
-----------------------