- 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 get the following-sibling element for a table type of structure in Selenium?
We can get the following-sibling element for a table type of structure in Selenium webdriver. A following-sibling is used to determine the siblings of the context node.
Also, these siblings should be present at the same hierarchy of the current node and share the same parent. Let us take an instance of a table with table tag having multiple children with tag tr.
Then let us try to identify the elements in the third row highlighted on the below image from the first row which contains the table headers.
Syntax
//table[@id='table1']/tbody/tr[1]/following-sibling::tr[2]/td
Here, we are locating the third row in the table, but we have provided followingsibling::tr[2] since we are locating the third row from the first row.
Example
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 FollowingSiblingTable{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver. get("https://sqengineer.com/practice-sites/practice-tables-selenium/"); //identify third row from first row in form of list List<WebElement> l = driver.findElements(By.xpath ("//table[@id='table1']/tbody/tr[1]/following-sibling::tr[2]/td")); //iterate list to get text of third row elements for (int i = 0; i<l.size(); i++) { String colVal = driver.findElements(By.xpath ("//table[@id='table1']/tbody/tr[1]/following-sibling::tr[2]/td")) .get(i).getText(); System.out.println("Values at third row after are : " + colVal); } driver.close(); } }
Output
- Related Articles
- Find next sibling element in Selenium, Python?
- What is following-sibling in Selenium?
- Sibling of a list element in JavaScript?
- How to get attribute of element from Selenium?
- How can we get the structure of a MySQL view as we can get the structure of a MySQL table?
- How to get the values of a particular row in a table in Selenium with python?
- Java Program to get the next sibling in a JTree
- How to get the screenshot of a particular element in the page in Selenium with python?
- How to get an attribute value of an element in Selenium Webdriver?
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- Java program to get the previous sibling from a JTree
- How to get coordinates or dimensions of element with Selenium Python?
- How to get text from Selenium element WebElement object?
- How to get Selenium to wait for ajax response?
- How to get text from each cell of an HTML table using Selenium?

Advertisements