petseda petseda - 1 year ago 68
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download