My Table (shortened and formatted for better viewability ) :
String htmlStr =
"<table class="xt">
<tbody>
<tr><th class="bg-warning" colspan="3">-</th></tr>
<tr><th class="bg-warning">2014-08-29</th><td>0</td><td>0.00</td><td>0.00</td></tr>
</tbody>
</table>"
My code:
org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
xt = doc.select("table.xt").first();
thCols = xt.select("tr").eq(1).select("th").size();
tdCols = xt.select("tr").eq(1).select("td").size();
Outcome
System.out.println(thCols); // prints 1
System.out.println(tdCols); // prints 2 - whereas I am expecting 3
Why would this be happening? I did my searches - but the best I could find were comments on how jSoup expects properly constructed HTML5. However, the table above does seem to be compliant, unless I have missed something badly. What else could be the reason?
This is a bug in the latest Jsoup 1.8.3, introduced in version Jsoup 1.8.2. If you switch back to 1.8.1 it should work as expected. Another solution would be to create a single selector for the number of tds:
String htmlStr = ""
+"<table class=\"xt\">"
+" <tbody>"
+" <tr><th class=\"bg-warning\" colspan=\"3\">-</th></tr>"
+" <tr><th class=\"bg-warning\">2014-08-29</th><td>0</td><td>0.00</td><td>0.00</td></tr>"
+" </tbody>"
+"</table>";
Document doc = Jsoup.parse(htmlStr);
Element xt = doc.select("table.xt").first();
int thCols = xt.select("tr").eq(1).select("th").size();
int tdCols = xt.select("tr").eq(1).select("td").size();
int tdCols2 = xt.select("tr:eq(1)>td").size();
System.out.println(thCols); // prints 1
System.out.println(tdCols); // prints 2 - whereas I am expecting 3
System.out.println(tdCols2); // prints 3 as expected
This may be the same bug as described here
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