简体   繁体   English

如何检查Jsoup中元素的可见性?

[英]How to check the visibility of an element in Jsoup?

I cannot find any direct method like isDisplayed() in Jsoup Element. 我在Jsoup Element中找不到像isDisplayed()这样的直接方法。 I can check the input with type = "hidden" by using the following code. 我可以使用以下代码检查type = "hidden"的输入。

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

But I need the CSS hidden to be captured as well. 但是我需要隐藏的CSS才能被捕获。 And also the inherited hidden elements. 还有继承的隐藏元素。

Pshemo said it already in his comment: JSOUP is not a JavaScript interpreter. Pshemo说他已经在评论中说:JSOUP不是JavaScript解释器。 And JSOUP does not combine external CSS info into html. 并且JSOUP没有将外部CSS信息组合到html中。 JSOUP just interprets html, and it is very good at this. JSOUP只是解释html,它非常擅长。 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. 您还可以访问互联网并使用JSOUP加载html页面,但这确实是它的限制。

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 . 对于Java,有很好的绑定到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. 我有很好的经验,使用selenium访问Web内容,然后切换到JSOUP实际抓取。 In your case you can use the powerful webdriver API directly to find out if an element is hidden or not. 在您的情况下,您可以直接使用强大的webdriver API来查明元素是否隐藏。

Selenium webdriver is able to work with Firefox, Chrome and a bunch of other browsers. Selenium webdriver能够与Firefox,Chrome和许多其他浏览器一起使用。 If you need a lightweight alternative you may use a headless browser. 如果您需要轻量级替代品,您可以使用无头浏览器。 For that there exists PhantomJs , which is exellenttly supported by selenium. 为此,存在PhantomJs ,它很好地受到硒的支持。 Or HTMLUnit , which is even lighter and uses the Java Rhino interpreter for JavaScript. HTMLUnit ,它甚至更轻,并使用Java Rhino解释器的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. 只是没有JSOUP,虽然它是一个很棒的图书馆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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