Skip to main content

Posts

Showing posts from July, 2011

Selenium2 - Read a Table

If I have a table which is designed in this manner:









If you have an ID for the table its really good to catch the table else you will have to do alot of manipulation with the tables you find...

// Here I try to identify the table using its ID.
WebElement table = webdriver.findElement(By.id("tableID"));
// Here I try to identify the table using a simple xpath
WebElement table = webdriver.findElement(By.xpath("/html/body/table/tbody"));

// Get all the rows of the table
List rows = table.findElements(By.tagName("tr"));
Iterator i = rows.iterator();

System.out.println("-----------------------------------------------");
// Iterate over the rows of the table
while(i.hasNext()) {
WebElement row = i.next();
// Find all the columns of the row
List columns = row.findElements(By.tagName("td"));
Iterator j = columns.iterator();

System.out.print(" | ");
// Iterate over the columns of the particular row

Selenium: Get DropDown Options

How to get the options of a dropdown list?

Here are the ways to get it:
Option 1:
- There is a built in function in selenium getselectoptions() which can be called to get the list of options available for the select tag. For ex: string[] dropDownItems = selenium.GetSelectOptions("//select[@id='someid']");

Option 2:
- If you want to get the text of the dropdown items which are in this format

Option One
Option Two

then you can get the text for the options using:

var iCount = (int) selenium.GetXpathCount("//select[@id='someid']/option");
var optionsList = new List();
for (int i = 1; i <= iCount; i++)
{
String option = selenium.GetText("//select[@id='someid']/option[" + i + "]");
optionsList.Add(option);
}

Option 3:
To get the id for the select options. We need the following code:

var iCount = (int) selenium.GetXpathCount("//select[@id='someid']/option");
var optionsList = new List();
for (int i = 1; i <= iCount; …