xpath to get rows inside a table - java

I have a html table like:
<table ..... class="cars">
<tr class="item-odd">
...
</tr>
<tr class="item-even">
</tr>
</table>
How could I get the table rows using xpath?
//tr[contains(#class, ???)
can I use OR in there somehow to say item-odd|item-even

You can get the rows like this:
//table[#class='cars']/tr
To get the item-odd rows, you would do this:
//table[#class='cars']/tr[#class='item-odd']
Yes, you can use or, e.g.:
//table[#class='cars']/tr[#class='item-odd' or #class='item-even']
See http://www.w3.org/TR/xpath for more details.

Related

Selenium - Skip a table

I'm trying to pull some data out of a table using Selenium. The page I am trying to pull the data from has 2 separate tables on the page, and the data I need is in the second table. I have pointed to the second table using it's ID, but I keep getting back the data from the first table. Here is the piece of code I am using to pull from the table.
WebElement table = driver.findElement(By.id("tblSBResults"));
String date = table.findElement(By.xpath("//tbody/tr[1]/td[1]")).getText();
I have also tried the xpath as //table/tbody/tr[1]/td[1].
And here is what the table I'm trying to pull from looks like.
<table>
<tbody>
<tr>
<td>some data</td>
<td>more data</td?
</tr>
</tbody>
</table>
...
...
<table id = "tblSBResults">
<tbody>
<tr>
<td>6/9/2015</td>
<td>More data that I need</td>
<td>more needed data</td>
</tr>
<tr>
<td>more etc</td>
<td>I think you get the point</td>
</tr>
</tbody>
</table>
Any ideas would be greatly appreciated. I'm using the Java version of Selenium by the way.
Thanks!
Using // will start searching at top of page.
Try this:
WebElement table = driver.findElement(By.id("tblSBResults"));
String date = table.findElement(By.xpath("tbody/tr[1]/td[1]")).getText();

Selenium WebDriver Java / JUnit - Getting table element

I have the following table:
<table>
<tbody>
<tr>
<td>
</td>
<td>
User1
</td>
</tr>
<tr>
<td>
</td>
<td>
User2
</td>
</tr>
</tbody>
</table>
I want to find the a-tag of the tr, where the data User2 is in the same row. I know that I can find an a-tag with partial link like findElement(By.partialLinkText("/ResetPassword/")); (the number 2 can change, so I canĀ“t use it as seperator). But I need to seperate it by User. Is there a solution like tr.td.text("User2") > findElement(By.partialLinkText("/ResetPassword/"));?
This XPath should do the trick for you. .//tr[td[normalize-space(text())='User2']]//a Just keep changing "User2" part with the desired user value.
Hi I was wondering if you could use the .getValue() statement with /#a at the end of the xpath to locate the attribute "a".
Main thing to do is find a bulletproof xpath to locate the User2 row once you have done that finding the value of "a" should be easy enough.
I hope this helps
U can try something like this(not sure though)-
List<WebElement> list=table.findElements(By.tagName("tr"));
List<WebElement> tdvalues=null;
for(WebElement web:list){
tdvalues=web.findElements(By.tagName("td"));
if(tdvalues.contains("User2")){
System.out.println(tdvalues.get(0).getText());//0th position contains the link
}
tdvalues.clear();
}

Select component, which has point in id with JSoup

I want to select this table:
<table class="pane sortable" id="game.scores">
<tbody>
<tr>
.....
</table>
I tried to do this:
Elements el = doc.select("table#game.scores");
System.out.println(el.html());
But it does not work.
Try this selector,
table[id=game.scores]
You can verify various selectors without running java code, here

How to iterate over <td> tags with condition using jsoup

I am able get all text with in tags but I want to access only specific td tags.
Eg.I want to get data of second cell text whose first cell html contains attribute
a name="manufacturer"
or Content.I am using Jsoup.
<tabel>
<tr>
<td><a name="Manufacturer"></a>manufacturer</td>
<td>happiness</td>
</tr>
<td>manuf</td>
<td>hap</td>
</tr>
<tr>
<td>tents</td>
<td>acd</td>
</tr>
<tr>
<td><a name="Content"></a>Contents</td>
<td>abcd</td>
</tr>
</tabel>
I am using the code ..
doc.select("a[name=Manufacturer]");
..but its giving me the reference of cell one ,I need to go to cell two get cell two text
You need to use selector like [attr=value]: elements with attribute value, e.g. [width=500].
Take a look at official documentation Selector Syntax

How i can find a elements with tag table in a html page

I have the html page, and me need find some elements with tag a 'table', but such tables maybe more than one, and they don't have id or class name.
Example:
<table>
<tbody>
<tr>
.....
<table>
<tbody>
<tr>
.....
<table>
<tbody>
<tr> <-------BINGO!
.....
How i can it's do with help HtmlUnit?
Thanks...
Use HtmlPage.getElementsByTagName("table") and iterate over the returned list of HtmlTable objects until you find the table you want to find.

Categories