29 – Element Handling in Selenium Part-4

Selenium Class 29: Element Handling in Selenium Part-4

9) Handle Drop down Box

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

Selenium WebDriver Steps:

driver.get(“http://www.gcrit.com/build3/create_account.php?osCsid=6rqlkhhibc3d4pvkdt6fafs4s1”);

boolean displayStatus = driver.findElement(By.name(“country”)).isDisplayed();
System.out.println(displayStatus);//true

boolean enabledStatus = driver.findElement(By.name(“country”)).isEnabled();
System.out.println(enabledStatus);//true

Select dropdown = new Select (driver.findElement(By.name(“country”)));
//dropdown.selectByIndex(6);

dropdown.selectByVisibleText(“India”);
Thread.sleep(4000);

List e = dropdown.getOptions();
System.out.println(e.size());
driver.close();

10) Handling Web Table / HTML Table

  • Check Displayed Status
  • Get / Return Cell Value
  • Return Rows Count
  • Return Cells count
  • Return Columns Count

Selenium WebDriver Test Script:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“file:///C:/Users/gcreddy/Desktop/HTMLExamples/htmlTable.html”);

boolean displayedStatus=driver.findElement(By.id(“students”)).isDisplayed();
System.out.println(displayedStatus);//true
WebElement studentsTable=driver.findElement(By.id(“students”));

List rows =studentsTable.findElements(By.tagName(“tr”));
int rowsCount=rows.size();
System.out.println(rowsCount);//3
//Or
System.out.println(rows.size());//3

List cells =studentsTable.findElements(By.tagName(“td”));
System.out.println(cells.size());//9

System.out.println(cells.size() / rows.size());//3

//Rows * Columns = Cells
//Cells/Rows=Columns
//Cells/Columns=Rows

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

//Or
System.out.println(driver.findElement(By.xpath(“.//*[@id=’students’]/tbody/tr[2]/td[2]”)).getText());

11) Handling Frames

  • HTML Frames are used to divide the Browser window in to multiple sections, where each section can load a separate HTML document
  • Frames are sections of web page displayed 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 frames directly.

In Automated Testing using Selenium WebDriver, first we need to switch to a frame from Top window then 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
Syntax:
driver.switchTo().frame(“String Frame Name”);
………………….
//Incorrect Test Steps

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“http://seleniumhq.github.io/selenium/docs/api/java/index.html”);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
………………….
//Correct Test Steps
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“http://seleniumhq.github.io/selenium/docs/api/java/index.html”);
//Switch to 3rd Frame from Top Window using index
driver.switchTo().frame(2);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
………………….
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“http://seleniumhq.github.io/selenium/docs/api/java/index.html”);

//Switch to 3rd Frame from Top Window using Frame Name
driver.switchTo().frame(“classFrame”);
driver.findElement(By.linkText(“org.openqa.selenium”)).click();
………………….
Example:

Test Requirement:

  • Launch the Web page, which 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, which has multiple frames
  • Switch from Top Window to 3rd Frame
  • Access/Operate an Element
  • Back to Top Window (default)
  • Switch from Top Window to first Frame
  • Access/Operate an Element

Selenium WebDriver Test Script:

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
//Launch the Web page, which has multiple frames
driver.get(“http://seleniumhq.github.io/selenium/docs/api/java/index.html”);

//Switch from Top Window to 3rd Frame
driver.switchTo().frame(2);
Thread.sleep(4000);
//Access/Operate an Element
driver.findElement(By.linkText(“org.openqa.selenium”)).click();

//Back to Top Window (default)
driver.switchTo().defaultContent();

//Switch from Top Window to first Frame
driver.switchTo().frame(0);
Thread.sleep(4000);
//Access/Operate an Element
driver.findElement(By.linkText(“com.thoughtworks.selenium”)).click();
driver.close();
………………………..
//Using Frame Name
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
//Launch the Web page, which has multiple frames
driver.get(“http://seleniumhq.github.io/selenium/docs/api/java/index.html”);

//Switch from Top Window to 3rd Frame
driver.switchTo().frame(“classFrame”);
Thread.sleep(4000);
//Access/Operate an Element
driver.findElement(By.linkText(“org.openqa.selenium”)).click();

//Back to Top Window (default)
driver.switchTo().defaultContent();

//Switch from Top Window to first Frame
driver.switchTo().frame(“packageListFrame”);
Thread.sleep(4000);
//Access/Operate an Element
driver.findElement(By.linkText(“com.thoughtworks.selenium”)).click();
driver.close();

12) Handling inline Elements

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“https://www.google.co.in”);
driver.findElement(By.xpath(“.//*[@id=’gbwa’]/div[1]/a”)).click();
driver.findElement(By.cssSelector(“#gb36 > span.gb_k”)).click();
………………………………………………………………