简体   繁体   中英

Extracting Element from html document for android app using Jsoup

I am trying to extract the text from this id to use in an android app I am trying to build.

<div id="114561_435450">CSE423 - DMH - UB30301<br></div>

As I am using Jsoup Library I already tried using getElementById("114561_435450") and div[id=114561_435450].text() . I am pretty much frustrated right now. Please any kind of help is appreciated. Thanks in advance.

Using purely Javascript, the following should work:

getElementById("114561_435450").innerHTML

If you can use jQuery, the following should also work:

$("#114561_435450").html()

I see two possible reasons, why your code may not work

  1. The id is changing with each request to the page. This is easy to check for, just load the url again in a browser and see if the id changed. do not forget to clear cache and cookies between tests. If the id changes indeed, you need to find out more about the structure of the document in order to find the correct div.

  2. The content of the document may be filled in by AJAX and thus not directly accessible to you. If this is the case you can find out by looking at a) the url loaded via curl or print it out from the Jsoup document and b) the network traffic when the page loads (Developer tools in Chrome or Firefox). If this is the case you should find out the URL of the AJAX call and get this instead of the original url.

Another solution to your problem may be the use of Selenium Webdriver. With this you actually remote control a real browser which should be perfectly able to execute any JavaScript that populates the DOM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM