我正在尝试获取 CSS 属性“content”的值:
WebElement plusOrMinusSign = currentEntityTypeLevel1.findElement(
By.xpath("i[@class='tree-branch-head']::after"));
System.out.println(plusOrMinusSign.getCssValue("content"));
但是,我得到一个错误:字符串 'i[@class='tree-branch-head']::after' 不是有效的 XPath 表达式。
似乎“::after
”没有被识别。
HTML:
<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)">::after
</i>
CSS:
i:after {
content: "-";
}
知道如何获取“内容”的值吗?
最佳答案
您正在使用 By.xpath
但 i[@class='tree-branch-head']::after
不是有效的 XPath,它是一个混合XPath 符号 (i[@class='tree-branch-head']
) 和 CSS (:after
)。
您应该使用 By.cssSelector
和一个有效的 CSS 选择器,例如 i.tree-branch-head:after
。如果 Selenium 接受伪元素,这将起作用,但它不接受。
要解决此问题,您可以使用 Chromium,它会生成额外的假元素 ::after
和 ::before
,或者使用 Javascript 提取器,如中所述https://stackoverflow.com/a/28265738/449288 .
关于css - 提取 :after using XPath 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40885637/