I have been trying to access a specific element from a dropdown menu on a Steam leaderboard page. Is there a good way to access a specific 'option value'? I have tried the following code and it isn't grabbing any value (debugger shows levelName as "").
String url = "http://steamcommunity.com/stats/592300/leaderboards/1900835";
Document document = Jsoup.connect(url).get();
String levelName = document.select("div#leaderHeader > option:contains(1900822)").val();
System.out.println(levelName);
There are few mistakes in your code:
:contains
doesn't check attributes, but text generated by element. If you want to find <option value="1900822">someText</option>
using value="1900822"
then you can use [attr=value]
selector like option[value=1900822]
(without quotes)parent > child
but <option ...>
is child of <select ...>
element, not <div id="leaderHeader">
. This div is its ancestor, but it is not parent, so remove that >
val()
method here, but I am guessing you want to use text()
.So you probably want:
String levelName = document.select("div#leaderHeader option[value=1900822]").text();
Just notice that there are some non-breaking spaces
in result. You may want to replace them with simple spaces and after that trim leading and trailing ones.
levelName = levelName.replace((char)160,' ').trim();