- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find specific lines in a table using Selenium?
We can find specific lines in a table with Selenium webdriver. A table is identified in an html document with <table> tag. Each table comprises a <tr> tag that represents rows and a <td> tag to represent columns. To traverse through table rows and columns we use the method findElements().
To count the total number of rows in table we use size() method available for list −
List <WebElement> l=driver.findElements(By.tagName("tr")); int s= l.size();
To count the total number of columns in table we use size() method available for list −
List <WebElement> m=driver.findElements(By.tagName("td")); int t= m.size();
Let us consider the below table and get the value of TYPE having AUTOMATION TOOL as UFT.
The html structure of a table is described below −
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class TableHandling{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = " https://sqengineer.com/practice-sites/practice-tables- selenium/"; driver.get(url); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify table WebElement mytable = driver.findElement(By.xpath("//table[@id='table1']/tbody")); //identify rows of table. List<WebElement> r_table = mytable.findElements(By.tagName("tr")); String b_xpath = "//table[@id='table1']/tbody/tr["; String a_xpath = "]/td[1]"; //calculate rows number with size() int rs_c = r_table.size(); //iterate rows of table and check matching condition for (int r = 2;r <= rs_c; r++) { String n = driver.findElement(By.xpath(b_xpath + r + a_xpath)).getText(); if(n.contains("UFT")){ // get text of matching cell String celtxt=driver.findElement(By.xpath("//table[@id='table1']/tbody/tr["+r+"]/td[2]")).getText(); System.out.println("The cell data at particular row is:" + celtxt); break; } } driver.quit(); } }
Output
- Related Articles
- How to scroll a specific DIV using Selenium WebDriver with Java?
- How to use a specific chrome profile in selenium?
- How to assign specific colors to specific cells in a Matplotlib table?
- How can I close a specific window using Selenium WebDriver with Java?
- How can I capture network traffic of a specific page using Selenium?
- How to find Elements in a Webpage using JavaScript in Selenium?
- How to retrieve a specific number of lines from files in PowerShell?
- How to display vertical grid lines in a table with Java?
- How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
- How to set a cookie to a specific domain in selenium webdriver with Python?
- How to exclude a specific row from a table in MySQL?
- Moving mouse pointer to a specific location or element using C# and Selenium
- How to find an element using the “XPath” in Selenium?
- How to find a radio button element by value using Selenium?
- How to search a MySQL table for a specific string?

Advertisements