如何从URL中提取WebPage的相对Xpath

时间:2018-06-12 17:32:13

标签: dom xpath jsoup html-parsing sample

我正在开发一个程序来列出网页内的元素及其相应的相对xpath。 使用Java和JSoup,我想提取为任何给定webPage内的所有元素动态创建的相对Xpath。一个完整的小工作实用程序肯定会帮助我。

我想要类似的内容:

//*[@id="menu-item-13686"]/a

示例输出:

Element Or Node or component Name: xxxx AND Xpath = //*[@id="menu-item-13686"]/a

谢谢

1 个答案:

答案 0 :(得分:1)

我认为你可以从这开始。

  

在jOOX版本中修复了问题 - 1.6.1 使用Java 10编译

     

参考https://github.com/jOOQ/jOOX/issues/158

下面的代码片段选择所有元素,并为每个元素打印出节点名称,标记名称和CSS选择器以及将唯一选择此元素的Xpath。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.joox.selector.CSS2XPath;

public class TestParser {

public static void main(String[] args) {

    try {
        Document doc = Jsoup.connect("https://theuserisdrunk.com/").get();
        Elements elements = doc.select("*");
        for (Element element : elements) {
            String path = CSS2XPath.css2xpath(element.cssSelector(), true);
            System.out.println("Node name : " + element.nodeName());
            System.out.println("      Tag : " + element.tagName());
            System.out.println("      CSS : " + element.cssSelector());
            System.out.println("    XPath : " + path + "\n");

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

示例输出:

 Node name : div
      Tag : div
      CSS : #mc-embedded-subscribe-form > div.clear:nth-child(4)
    XPath : //*[@id='mc-embedded-subscribe-form']/div[@class='clear' or starts-with(@class, 'clear ') or ' clear' = substring(@class, string-length(@class) - string-length(' clear') + 1) or contains(@class, ' clear ')][count(preceding-sibling::*) = 4 - 1]

Node name : input
      Tag : input
      CSS : #mc-embedded-subscribe
    XPath : //*[@id='mc-embedded-subscribe']

Node name : p
      Tag : p
      CSS : #mc_embed_signup > p.intern:nth-child(2)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 2 - 1]

Node name : a
      Tag : a
      CSS : #mc_embed_signup > p.intern:nth-child(2) > a
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 2 - 1]/a

Node name : p
      Tag : p
      CSS : #mc_embed_signup > p.intern:nth-child(3)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 3 - 1]

Node name : i
      Tag : i
      CSS : #mc_embed_signup > p.intern:nth-child(3) > i
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 3 - 1]/i

Node name : p
      Tag : p
      CSS : #mc_embed_signup > p.intern:nth-child(4)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 4 - 1]

Node name : a
      Tag : a
      CSS : #mc_embed_signup > p.intern:nth-child(4) > a:nth-child(1)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 4 - 1]/a[count(preceding-sibling::*) = 1 - 1]

Node name : a
      Tag : a
      CSS : #mc_embed_signup > p.intern:nth-child(4) > a:nth-child(2)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 4 - 1]/a[count(preceding-sibling::*) = 2 - 1]

Node name : i
      Tag : i
      CSS : #mc_embed_signup > p.intern:nth-child(4) > i
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 4 - 1]/i

Node name : a
      Tag : a
      CSS : #mc_embed_signup > p.intern:nth-child(4) > a:nth-child(4)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 4 - 1]/a[count(preceding-sibling::*) = 4 - 1]

Node name : a
      Tag : a
      CSS : #mc_embed_signup > p.intern:nth-child(4) > a:nth-child(5)
    XPath : //*[@id='mc_embed_signup']/p[@class='intern' or starts-with(@class, 'intern ') or ' intern' = substring(@class, string-length(@class) - string-length(' intern') + 1) or contains(@class, ' intern ')][count(preceding-sibling::*) = 4 - 1]/a[count(preceding-sibling::*) = 5 - 1]

Node name : script
      Tag : script
      CSS : html > body > script:nth-child(3)
    XPath : //html/body/script[count(preceding-sibling::*) = 3 - 1]

Node name : script
      Tag : script
      CSS : html > body > script:nth-child(4)
    XPath : //html/body/script[count(preceding-sibling::*) = 4 - 1]