Hi There...
This API introduces a WebTable class (The first utility), that will help in dealing with an HTML table
Little story - workaround , done smartly :)
While I come across a Table in an HTML page, accessing the table cell content using Web Driver script makes the code look silly with lots of XPATH content (Most of the times ID is not available for cell, hence we prefer XPATH ). Do you had this experience ? if not , lets take a sample example :)
A Sample Table as shown in image : from WordPress
If you want to get the content of the first and second cell from the Header Section then the Web Drvier code would be something like
String firstCellOfHeader= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/thead/tr/th[2]/a/span")).getText();
String secondCellOfHeader= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/thead/tr/th[3]/a/span")).getText();
If you want to get the content of the first and second cell from the first row of Body Section then the Web Drvier code would be something like
String firstCellOfBody= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/tbody/tr/td/strong")).getText();
String secondCellOfBody= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/tbody/tr/td[2]")).getText();
The same code, when used with the Utility API, there is no need for having XPATH or any Locator to identify the cell of a table.
The equivalent code would be ...
/*Step1: Identify the Table Element*/
WebElement postsTable = driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table"));
/*Step2: Pass the Table Element to the WebTable Utility Class provided in the API*/
WebTable table = WebTable.getTable(postsTable);
/*Once the above two steps are done, now you can access any cell by passing its cell index : starts from 0th Index*/
String firstCellOfHeader= table.getTHead().getRow(0).getHeaderCell(1).getText();
String secondCellOfHeader= table.getTHead().getRow(0).getHeaderCell(2).getText();
String firstCellOfBody= table.getTBody().getRow(0).getCell(0).getText();
String secondCellOfBody= table.getTBody().getRow(0).getCell(1).getText();
This approach Benefits few things
- Code looks more clean and readable
- No need for having locator to identify the Cell
- Easy scripting and debuging
Download Locations:
Download ATU Selenium Utilities - WebTable API (included from version1 onwards)
A lot more utilities will be added to this API. Happy Scripting :)
LinkedIn Group:
you can join our LinkedIn group here for more collaborative discussions: ATU LinkedIn Group
Give our tools a Feedback:
We are constantly working on several projects, we would like to have a feedback on all the tools so that we can prioritize, introduce new features, enhancing them etc. This will also help us in understanding the usage of these tools and lift us to develop further more utilities.Feedback page : ATU Utilities User Feedback
We love Automation; we love to keep its utilities Open Source.
– Automation Testing Utilities
Hi,
ReplyDeleteCan we get the table row by one of its cell value instead of index?
Regards,
Excellent blog, good to see someone is posting quality information. Thanks for sharing this useful information. Keep up the good work diamond drilling contractors
DeleteIts not implemented in this api. Imagine 10 different cells have similar content, in this case we should return more than one row isn't it.
ReplyDeleteRegards
AT
yes, with one parameter, there will be a problem but what if it take 2 parameters, index and cell value to uniquely identify it.
ReplyDeleteAnyways nice efforts indeed.
Regards,
with two parameters, by index I mean the "column index" as I have unique values in that particular column.
ReplyDeleteYou can create a method which can return row num. This api can be used to develop that method.
DeleteRegards
AT
Ok, I will try.
DeleteThanks
Hi,
ReplyDeleteCan you please tell me that how can we click an element/link inside the web table using this api ?
As explained in the post, find the table element, pass it to the webtable api method. Find the desired row and retrieve the element.
Delete#AT
I need to click on an element inside the Cell, I can get the row element , but how can click on specific cell
ReplyDeleteKaruna,
DeletegetRow(0).getCellElement(index) will return the Web Element.
pass the index of the column
#AT
1) can you please tell how can we get total columns ?
ReplyDelete2) Is there any way to get the total rows if my table results has pagination ?
Hi,
Delete1) Use the API method "getCoulmnCount()" to get count of columns.
2) Not sure with pagination, we will be glad to know if you can check it to see if works
#AT
how to configure this API , if i want use it
ReplyDeletecan't access the cell value by using table.getTHead().getRow(0).getHeaderCellElement(4).getText() or even table.getTHead().getRow(0).getCellElement().getText(). Column size is 8 but its showing 2 . what to do ?
ReplyDeletewhen i am using table.getTHead().getRow(0).getHeaderCellElement(0).getText() its working but when increasing cell index its not working
How to get the value of textbox from webtable
ReplyDeleteHello Admin, thank you for the informative post. At Fita academy, we provide the best IT trainings for various courses. You can check our Fita Chennai complaints page to know more about our quality and standards.
ReplyDeleteEmbedded system training in chennai
ReplyDeleteEmbedded Course training in chennai
Matlab training in chennai
Android training in chennai
LabVIEW training in chennai
Arduino training in chennai
Robotics training in chennai
Oracle training in chennai
Final year projects in chennai
Mechanical projects in chennai
ece projects in chennai
PLC training in chennai
ReplyDeleteAutomation training in chennai
Best plc training in chennai
PLC SCADA training in chennai
Process automation training in chennai
Final year eee projects in chennai
VLSI training in chennai
Hi ATU
ReplyDeleteThis approach is time taking if you have 1000 + rows in table , so instead of this we can use Jsoup library that gives all table records within seconds :)
Aman Saraf Jain