Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the element within element in selenium

I am creating a framework for the data validation using selenium. The issue I am struggling with is I want to locate the element "td"(html tag) within element "tr"(html tag) . This is the code I have written.

Iterator<WebElement> i = rows.iterator();
While(i.hasnext()){
List<WebElement> columns = row.findElements(By.tagName("td"));
for(WebElement s:columns)
{
    System.out.println("columnDetails : "+s.getText().toString());
}
if(columns.isEmpty())
{
    ElementNotFoundException e = new ElementNotFoundException("No data in table");
    throw e;
}
Iterator<WebElement> j = columns.iterator();// does some other work
ClusterData c = new ClusterData(); // does some other work
ClusterDataInitializer.initUI(c, j, lheaders); // does some other work
CUIData.put(c.getCN(), c); // does some other work
}

Now the issue with this is:

The Data from table which i am trying to fetch in arraylist

I am trying to fetch the data from the rows(see table data) in arraylist and use that arraylist further. Currently whats happening is the data for column header is fetched at start of which I have no use.I only want the rows's data. I am not able to determine the proper way to collect the data of table rows only. if xPath of the table will help you understand it properly then here are the details :

Table header xPath of cluster name column:

/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/table/tbody/tr/td[2]/div/div[2]

Table row (Table Data) xPath of test cluster 01:

/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[3]/div[2]/div/table/tbody/tr/td[2]/div/div/a

Please let me know if you need anything else.

I am using the following code to extract row data from table.

List<WebElement> rows = getElement(driver,sBy,"table_div_id").findElements(By.tagName("tr"));

where sBy = By.id and table_div_id = id of div in which table is present. This extracts all the rows into arraylist and then i am using code to extract the row data into another arraylist. It is where I am stuck.

like image 260
Avataar17 Avatar asked Feb 05 '14 13:02

Avataar17


1 Answers

Each row from the table is in its own "table" tag so following things are not working :-

 List<WebElement> rows = driver.findElements(By.xpath("//div[@id = 'table_div_id']//tr"));
 List<WebElement> columns = row.findElements(By.xpath("./td"));

or the approach I used for the previous release of product i.e.

List<WebElement> columns = row.findElements(By.tagName("td"));

So, I used following approach which enabled me to capture all of the visible rows from the table.

List<WebElement> columns = row.findElements(By.xpath(".//table[@class='gridxRowTable']/tbody/tr"));

But after that I faced another issue that is since this table was implemented using dojo, the scrolling was impossible and Selenium was only able to capture the visible rows , so to overcome this I zoomed out in the browser using selenium. This is how i achieved my goal of getting the data.I believe others might have provided me answer if i would have shared some more details. Still , sorry about that and hope my answer helps you all.

like image 70
Avataar17 Avatar answered Oct 31 '22 22:10

Avataar17