XPath中的CSS选择器 [英] CSS selector inside XPath
问题描述
是否可以在XPath中指定CSS选择器?本质上,我想找到同时满足.myClass div
和div[contains(., 'Hello!')]
的元素.
Is it possible to specify a CSS selector inside an XPath? Essentially, I want to find elements that satisfy both .myClass div
and div[contains(., 'Hello!')]
.
推荐答案
不,您不能将CSS选择器语法和XPath语法组合到单个表达式中.两种语言都没有任何功能可以让您从另一种语言中获取表达式.
No, you can't combine CSS selector syntax and XPath syntax into a single expression. Neither language has any feature that lets you feed an expression from the other.
您只能将CSS选择器作为XPath 并入XPath中,这意味着您需要自己对其进行翻译.在您的情况下,这是生成的XPath表达式:
You can only incorporate the CSS selector into the XPath as XPath, which means you'll need to translate it yourself. In your case, here's the resultant XPath expression:
//*[contains(concat(' ', @class, ' '), ' myClass ')]//div[contains(., 'Hello!')]
当然,这取决于您可以使用CSS选择器或XPath完成什么.例如,如果如果未从CSS3中删除:contains()
伪类,则您将能够使用这个CSS选择器也是如此:
Of course, this depends on what you can accomplish with CSS selectors or XPath. For example, if the :contains()
pseudo-class hadn't been dropped from CSS3, you would have been able to use this CSS selector just as well:
.myClass div:contains('Hello!')
但是它已从规范中删除,因此您只能使其用作jQuery选择器或Selenium CSS定位器.
But it's been removed from the spec, so you'll only be able to get it to work as a jQuery selector or a Selenium CSS locator.
这篇关于XPath中的CSS选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!