24

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.

2
  • 2
    That looks like the worst XPath I have ever seen! Surely you can make it simplier and more reliable than that. Commented Feb 5, 2014 at 20:54
  • I have copied it using firebug and pasted it as it is. Cant help it if the xPath is like this. This is the way the product is designed. My task is to create an automation framework. But if you give me some pointers regarding how I can make it simpler so that you can understand, I will definitely try. :) Commented Feb 6, 2014 at 4:26

4 Answers 4

22

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.

Sign up to request clarification or add additional context in comments.

Comments

13

instead of

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

try using

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

Check if this helps. This should give you the td elements. If I have not understood your issue, let me know.

5 Comments

I tried your suggestion but it still only picks up the data for the table headers and I am not able to pick the data of table rows.
ok. tell me how you are getting the "rows" in this line, Iterator<WebElement> i = rows.iterator(); please provide the code how you are getting the rows.
Please see the post. Thanks.
ok so i assume you have a table and a div top of the table. then use this List<WebElement> rows = driver.findElements(By.xpath("//div[@id = 'table_div_id']//tr")); "driver" is the Webdriver here and this should give you all the rows in the table.
Thanks for help Paul. You helped me think differently. :)
1

You can use this way-

driver.findElement(By.Xpath("//table[@id=\"table1\"]/tbody/tr[2]/td[1]"));  

Regards,

Anuja

Comments

-2

Do you have selenium IDE installed? Perform storeText operation on the row you want to retrieve, then xpath will get populated in IDE. There will be multiple xpaths; the most reliable is xpath:position, use that to capture your rows.

And use firebug for better visibilty of your AUT.

Firebug and Selenium IDE are the most basic component of Selenium Framework development.

You can manipulate xpath as you want.

2 Comments

I did not use IDE since the code is entirely developed using Selenium Webdriver.There are other aspects which cannot be implemented using IDE. We did start out with IDE but then switched to selenium webdriver for better performance and enlarge the scope of testcases.
I dont think IDE has anything to do with WEbDriver development. It is just used for record and play. It is a helper but not a SUBSTITUTE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.