使用XPath之后:中提取内容 [英] Extracting content in :after using XPath
问题描述
我试图通过以下方式获取CSS属性"content"的值:
I am trying to get the value of a CSS attribute "content" with:
WebElement plusOrMinusSign = currentEntityTypeLevel1.findElement(
By.xpath("i[@class='tree-branch-head']::after"));
System.out.println(plusOrMinusSign.getCssValue("content"));
但是,我收到一个错误:The string 'i[@class='tree-branch-head']::after' is not a valid XPath expression.
However, I get an error: The string 'i[@class='tree-branch-head']::after' is not a valid XPath expression.
似乎无法识别"::after
".
HTML:
<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)">::after
</i>
Css:
i:after {
content: "-";
}
有人知道如何获得内容"的价值吗?
Any idea how to get the value of "content"?
推荐答案
您正在使用By.xpath
,但是i[@class='tree-branch-head']::after
不是有效的XPath,它是XPath表示法(i[@class='tree-branch-head']
)和CSS(
You are using By.xpath
but i[@class='tree-branch-head']::after
is not a valid XPath, it is a mixture of XPath notation (i[@class='tree-branch-head']
) and CSS (:after
).
您应该使用By.cssSelector
和有效的CSS选择器,例如i.tree-branch-head:after
.如果Selenium接受了伪元素,那是行得通的.
You should use By.cssSelector
and a valid CSS selector, for example i.tree-branch-head:after
. This would work if Selenium accepted pseudo elements, which it does not.
要变通解决此问题,您可以使用Chromium,它会生成额外的伪元素::after
和::before
,或使用Javascript提取程序,如https://stackoverflow.com/a/28265738/449288 .
To work around this problem, you can either use Chromium, that generates extra fake elements ::after
and ::before
, or use a Javascript extractor as described in https://stackoverflow.com/a/28265738/449288.
这篇关于使用XPath之后:中提取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!