Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find specific lines in a table using Selenium?

Here is an example code:

<div id="productOrderContainer">
  <table class="table gradient myPage">

So this table being in productOrderContainer has several columns and depending on several things will have several rows which all have several columns. An example is:

What I want to do is to for example get the first row of this table. ( rows have id's such as: <td rowspan="1"> ) And then again for example in this rowspan look for a specific value in a specific <div>

So in psudo-code what I want to say is:

Get me the table, get me the n'th row, get me the value in <div id='something'>

like image 668
Koray Tugay Avatar asked Jan 07 '13 07:01

Koray Tugay


People also ask

How do I find the XPath of a row in a table?

Let's select an element in the web table and find its XPath. For Chrome, right-click and inspect the given element to find its XPath. To find the XPath of a UI element in Firefox, right-click on the desired element, go to “Inspect Element” to open the inspector which will help identify its XPath.

How do you select a value from a table in Selenium?

We can get all the values inside a table in Selenium with the help of find_elements method. The rows of a table are represented by <tr> tag in html code. To get all the rows, we shall use the locator xpath and then use find_elements_by_xpath method. The list of rows will be returned.

How do you iterate through a table in Selenium?

println(“Total number of rows : ” + rowsList. size()); The line prints the total number of rows. The above piece of code iterates through the list of rows, gets the list of columns of each row, iterates through each column (in second for loop) and gets the cell value using getText() method.


1 Answers

you can try following

int index = 0;
WebElement baseTable = driver.findElement(By.className("table gradient myPage"));
List<WebElement> tableRows = baseTable.findElements(By.tagName("tr"));
tableRows.get(index).getText();

You can also iterate over tablerows to perform any function you want.

like image 78
Prashant Shukla Avatar answered Sep 28 '22 01:09

Prashant Shukla