在使用XPath之后提取内容:

Ric*_*dom 4 css xpath pseudo-element selenium-webdriver

我试图通过以下方式获取CSS属性"content"的值:

WebElement plusOrMinusSign = currentEntityTypeLevel1.findElement(
    By.xpath("i[@class='tree-branch-head']::after"));
System.out.println(plusOrMinusSign.getCssValue("content"));
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误: 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>
Run Code Online (Sandbox Code Playgroud)

CSS:

i:after {
    content: "-";
}
Run Code Online (Sandbox Code Playgroud)

知道如何获得"内容"的价值吗?

gio*_*ele 6

您正在使用By.xpathi[@class='tree-branch-head']::after不是有效的XPath,它是XPath表示法(i[@class='tree-branch-head'])和CSS(:after)的混合体.

例如,您应该使用By.cssSelector和有效的CSS选择器i.tree-branch-head:after.如果Selenium接受了伪元素,那么这将起作用.

要解决此问题,您可以使用铬,产生额外的假元素::after::before,或使用JavaScript提取器描述/sf/answers/1978601691/.