【发布时间】: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