Select component, which has point in id with JSoup - java

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

Related

Selenium, Java. Need to select ancestor element inside table by Xpath

I have an HTML page containing the following code :
<table class="report" style="width:100%">
<tbody>
<tr>
<th/>
<th>Position Open
<br>
<span class="timestamp">27/7/2016 16:12:12</span>
</br>
</th>
<th>Position closed
<br>
<span class="timestamp">27/7/2016 16:12:42</span>
</br>
</th>
</tr>
<tr>
<td>
<span dir="ltr">EURJPY</span>
</td>
<td>116.098</td>
<td>116.156</td>
</tr>
</tbody>
</table>
On this page I have another table with the same class attribute "report" but only this table contains texts "Position Open" and "Position Closed".
I need to select elements containing the "EURJPY", "116.098" and "116.156" data.
These elements content is changing i.e. instead of "EURJPY" may appear "EURUSD" or "GBPCAD" etc.
I tried the following code:
driver.findElement(By.xpath("//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span")).getAttribute("textContent");
to get the first required field text but got the Invalid selector error.
Your XPath is close but there were a couple issues.
//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
You are searching for a SPAN that contains the text 'Position Open' when in fact it is a TH that contains the text.
//th[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
(//tr) should be corrected to //tr
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td/span
What you want is the text contained in the TD, not the SPAN. If you pull the text from the TD you can get the text you want from all three elements. If you pull the SPAN, then you will also need to pull the last two TDs. This way is just simpler.
...and finally, the TH contains more than just the text you are looking for. Use .contains() to get a match.
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td
So we take that XPath and put it into Java code and we get the below.
List<WebElement> tds = driver.findElements(By.xpath("//th[contains(text(),'Position Open')]/ancestor::table[#class='report']//tr[2]/td"));
for (WebElement td : tds)
{
System.out.println(td.getText());
}
There can be issues matching the text sometimes, use contains instead, try this selector
//th[contains(.,'Position')]/ancestor::table[#class='report']//tr[2]/td/span
You can use this xpath to locate the 3 <td> tags you are interest in
//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td
Using it will give you list of three elements, you can extract the text from them
List<WebElement> tds = driver.findElement(By.xpath"//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td");
String currency = tds.get(1).getText(); // this will be EURJPY
tds.get(2).getText(); // 116.098
tds.get(3).getText(); // 116.156

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();
}

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

xpath to get rows inside a table

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.

Categories