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'>
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With