petseda petseda - 4 months ago 12x
Java Question

How to get id from html Objects with Jsoup - Java

I want to find the id of html objects with Jsoup.

<object id="gamediv" </object>

I tried:

String startingURL = "";
try {
doc = Jsoup.connect(startingURL)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.timeout(1000*5) //it's in milliseconds, so this means 5 seconds.
} catch (IOException e) {
// TODO Auto-generated catch block

Elements get ="object");

for (Element elem : get){
if (get.attr("id") != null){

but nothing happens. Any help please?


First of all you can reduce your code to simple.

for (Element elem :"object[id]")) {

Secondly if doc doesn't contain object you are looking for, it means that it wasn't sent to it by server. There may be few reasons where most often ones are

  • incorrect user agent header,
  • this HTML code is generated by browser via JavaScript.

First case doesn't seem to apply here, so in case of dynamic content you should probably use other library since Jsoup is only parser, not browser emulator. If you are looking for more powerful tool take a look a web drivers like Selenium.