简体   繁体   中英

How to check the visibility of an element in Jsoup?

I cannot find any direct method like isDisplayed() in Jsoup Element. I can check the input with type = "hidden" by using the following code.

"HIDDEN".equals(elm.attr("type").toUpperCase())

But I need the CSS hidden to be captured as well. And also the inherited hidden elements.

Pshemo said it already in his comment: JSOUP is not a JavaScript interpreter. And JSOUP does not combine external CSS info into html. JSOUP just interprets html, and it is very good at this. Nothing much more but also nothing much less. You can also access the internet and load html pages with JSOUP, but that is really the limit of it.

About your problem: You should think hard if it is really needed to know if an element is visible or hidden. If it is in your context, you problably need a testing framework that behaves like a browser. For Java there are very good bindings to selenium webdriver . This drives a real browser to load and test pages. You can also scrape the content with selenium. I have good experience using both, selenium for accessing web content and then switching over to JSOUP for actually scraping. In your case you can use the powerful webdriver API directly to find out if an element is hidden or not.

Selenium webdriver is able to work with Firefox, Chrome and a bunch of other browsers. If you need a lightweight alternative you may use a headless browser. For that there exists PhantomJs , which is exellenttly supported by selenium. Or HTMLUnit , which is even lighter and uses the Java Rhino interpreter for JavaScript.

You see, there are quite some options to choose from to achieve what you want. Just not JSOUP, although it is a great library.

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