简体   繁体   English

Java Jsoup按钮选择

[英]Java Jsoup button selection

I just want to check whether given element exists or not, I have the following code: 我只想检查给定元素是否存在,我有以下代码:

Document doc = Jsoup.connect("http://www.gsmarena.com/news.php3?iPage=2").get();
    Elements button = doc.select("a > pages-next");
        if (!button.isEmpty()) {
            System.out.println("contains");
        } else {
              System.out.println("not contains");
        }

But it always returns false, here is also the element that I want to check: 但是它总是返回false,这也是我要检查的元素: 在此处输入图片说明

There are two issues here: 这里有两个问题:

  1. The right selector is .pages-next and not a > pages-next . 右边的选择器是.pages-next而不是a > pages-next
  2. You must provide the user-agent string, or else you'll get a page with different DOM structure then the one you get with your browser. 您必须提供用户代理字符串,否则您将获得一个页面的DOM结构与您通过浏览器获得的页面不同。

Use this code - 使用此代码-

Document doc = Jsoup.connect("http://www.gsmarena.com/news.php3?iPage=2")
             .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0")
             .get();
        Elements button = doc.select(".pages-next");
        if (!button.isEmpty()) {
            System.out.println("contains");
        } else {
              System.out.println("not contains");
        }

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

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