【问题标题】:how to use cssselector to convert css to xpath如何使用 cssselector 将 css 转换为 xpath
【发布时间】:2017-04-24 00:45:07
【问题描述】:

我想使用一些库将 css 选择器转换为 xpath。任何人都可以向我解释返回值的含义以及我如何覆盖到 xpath,如//div//*[class@""]

from cssselect import GenericTranslator

try
    sel = CSSSelector('div.w3-container.top')
except SelectorError:
    print('Invalid selector.')
print(sel)

sel:
'descendant-or-self::div[@class and contains(concat(\\' \\', normalize-space(@class), \\' \\'), \\' w3-container \\') and (@class and contains(concat(\\' \\', normalize-space(@class), \\' \\'), \\' top \\'))]'

【问题讨论】:

  • 这是xapth,不需要转换。由于它以descendant-or-self 开头,因此它期望您在要成为搜索根节点的节点上开始查询。你可以在前面加上// 来获得root权限。
  • 发布的代码不起作用(CSSSelector 未定义)。您能否更新一些对稍后查看此内容的其他人有用的内容?

标签: python-3.x selenium-webdriver css-selectors


【解决方案1】:

XPath(GenericTranslator().css_to_xpath('div.w3-container.top'))

这将返回给定 css 类的 XPath

访问link了解更多详情

【讨论】:

  • 它已经是 xpath... 这将创建一个 lxml XPath 对象,其中 xpath 是预先解析的,如果多次使用可能会更快。但不是必须的。
猜你喜欢
  • 1970-01-01
  • 2013-03-21
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多