CSS 提取内容:使用 XPath 后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40885637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting content in :after using XPath
提问by Rico Strydom
I am trying to get the value of a CSS attribute "content" with:
我试图通过以下方式获取 CSS 属性“内容”的值:
WebElement plusOrMinusSign = currentEntityTypeLevel1.findElement(
By.xpath("i[@class='tree-branch-head']::after"));
System.out.println(plusOrMinusSign.getCssValue("content"));
However, I get an error: The string 'i[@class='tree-branch-head']::after' is not a valid XPath expression.
但是,我收到一个错误: The string 'i[@class='tree-branch-head']::after' is not a valid XPath expression.
It seem the "::after
" is not recognised.
似乎::after
无法识别“ ”。
Html:
网址:
<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)">::after
</i>
Css:
css:
i:after {
content: "-";
}
Any idea how to get the value of "content"?
知道如何获得“内容”的价值吗?
回答by gioele
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.xpath
但i[@class='tree-branch-head']::after
不是有效的 XPath,它是 XPath 符号 ( i[@class='tree-branch-head']
) 和 CSS ( :after
) 的混合体。
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.
您应该使用By.cssSelector
一个有效的 CSS 选择器,例如i.tree-branch-head:after
. 如果 Selenium 接受伪元素,这将起作用,但它不接受。
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.
要解决此问题,您可以使用 Chromium,它会生成额外的假元素::after
和::before
,或者使用 Javascript 提取器,如https://stackoverflow.com/a/28265738/449288 中所述。
回答by Nitin Singh
Handle pseudo element in selenium webdriver
处理 selenium webdriver 中的伪元素
String script = "return window.getComputedStyle(document.querySelector('Enter root class name here '),':after').getPropertyValue('content')";
Thread.sleep(3000);
JavascriptExecutor js = (JavascriptExecutor) driver;
String content = (String) js.executeScript(script);
System.out.println(content);
Now use this content string in your assert command or anywhere you want to use , hope this will work for you... Good luck
现在在你的断言命令或任何你想使用的地方使用这个内容字符串,希望这对你有用......祝你好运