Accessing Web Elements Inside a table using xpath is very important to learn as you will need it to write automated Selenium Test Cases.As it is very difficult for developers to provide ids for each table element hence we need to use Xpath for accessing each cell element. So this post is very useful to understand accessing table using Selenium.
Accessing Table Cell Element -
HTML Code -
<html> <body> <table id="table1"> <table> <tr> <td>first </td> <td>second </td> </tr> <tr> <td>third</td> <td>four </td> </tr> <table> </body> </htmt>
Xpath Writing - Let us write xpath for clicking on cell with text "thrid"
As we know XPath locators start with a double forward slash "//" and then followed by the parent tag name. Since we are dealing with tables, the parent element will be the <table> tag. The first part of our XPath will be "//table".
The element with tab <tbody> is under <table> means <tbody> is child tag of of <table>. Now oue xpath will be modified as "//table/tbody".
Now inside <tbody> we need to access the second <tr> and first <td> so now xpath will be modofied as "//table/tbody.tr[2]/td[1]".
The element with tab <tbody> is under <table> means <tbody> is child tag of of <table>. Now oue xpath will be modified as "//table/tbody".
Now inside <tbody> we need to access the second <tr> and first <td> so now xpath will be modofied as "//table/tbody.tr[2]/td[1]".
WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[1]"));
Accessing Nested Table Cell Element -
HTML Code -
<html> <body> <!-- Outer Table --> <table id="table1"> <tbody> <tr> <td>first </td> <td>second </td> </tr> <tr> <td>third</td> <!-- Inner Table --> <table id="table2"> <tbody> <tr> <td>first 1</td> <td>second 2</td> </tr> <tr> <td>third 3</td> <td>four 4 </td> </tr> </tbody> <table> </tr> </tbody> <table> </body> </htmt>
Xpath Writing -
Xpath for locating "second 2"
WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[2]
+"/table/tbody/tr[1]/td[2]"));
Accessing table elment using attributes -
When the there are multiple tables in HTML code or if the tables are too much nested we can use any of the attributes provided in html code such as Id, Width, height, values etc. Refer the first html code where table id is given as table 1 then we can write xpath as per shown below
WebElement thirdCell = driver.findElement(By.Xpath("//table[@id=\"table1\"]/tbody/tr[2]/td[1]"));
Well done, every one can have to look at your blog to be good.
ReplyDeleteSelenium Training in Bangalore
Selenium Course in Bangalore
Best Selenium Training Institute in Bangalore
Selenium Training in Coimbatore
Selenium Course in Coimbatore
Selenium Classes in Coimbatore
I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai
DeleteThis post is very different and unique information. It's a very nice post and very attractive to me. Keep posting...
ReplyDeleteSpark Training
Embedded System Course Chennai
Linux Training in Chennai
Pega Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai
Corporate Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
ReplyDeleteRegards,
PHP Training in Chennai
PHP Course in Chennai
Tally Course in Chennai
ReactJS Training in Chennai
microsoft dynamics crm training in chennai
PHP Training in Chennai
PHP Course in Chennai
I have to thank for sharing this blog admin, really helpful to me.
ReplyDeleteTechnology
ejobsalert
very informative
ReplyDelete"best andaman honeymoon packages
andaman honeymoon tour packages "
nice post of this article. thanks for sharing.
ReplyDelete"best andaman honeymoon packages
andaman honeymoon tour packages "
Liên hệ đại lý Aivivu, mua vé máy bay tham khảo
ReplyDeletevé máy bay đi Mỹ giá rẻ 2021
khi nào có chuyến bay từ mỹ về việt nam
vé máy bay đi Los Angeles bao nhiêu tiền
thông tin chuyến bay từ canada về việt nam