【问题标题】:How to convert a CSS selector to XPath in Javascript?如何在 Javascript 中将 CSS 选择器转换为 XPath?
【发布时间】:2018-05-13 20:41:07
【问题描述】:

我正在寻找 Nokogiri::CSS.xpath_for() 的等价物。

我找到了 css2xpath,但是 Nokogiri 的结果确实有效,而 css2xpath 返回的选择器无效。

我正在寻找的结果是这样的:

["//div[@id = 'my_div']//*[position() = 1 and self::table]//*[position() = 1 and self::tbody]//*[position() = 2 and self::tr]//*[position() = 1 and self::td]//*[position() = 1 and self::table]//*[position() = 1 and self::tbody]//*[position() = 2 and self::tr]//*[position() = 2 and self::td]"]

【问题讨论】:

标签: javascript node.js xpath css-selectors


【解决方案1】:

我绝对推荐使用库将 CSS 转换为 XPath。

例如,试试css-to-xpath

【讨论】:

    【解决方案2】:

    你可以试试convert-cssxpath。 这个 npm 包还支持交互式控制台,并尽可能简化 XPath 表达式。

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 2012-09-16
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多