2016-11-30 101 views
1

我試圖得到一個CSS屬性「內容」與價值之後:提取內容:使用XPath

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.

這似乎是「::after 「不被認可。

HTML:

<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)">::after 
</i> 

的CSS:

i:after { 
    content: "-"; 
} 

不知道如何獲得的 「內容」 的價值?

+0

看到這篇文章https://davidwalsh.name/pseudo-element –

+0

嘗試'//我[@ class ='tree-branch-head'] :: –

+0

@Nikhil,我不熟悉使用javascript在一個硒項目裏面。你能給我一個指針/例子嗎? –

回答

3

您正在使用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接受了僞元素,那麼這將起作用,但它不會。

要解決此問題,您可以使用Chromium,該Chromium會生成額外的假元素::after::before,或使用https://stackoverflow.com/a/28265738/449288中描述的Javascript提取器。

+0

謝謝Gieole,你的'i.tree-branch-head:'幫助我走上了正確的軌道。這是retreiving'content'值的字符串值。 ('content.txt');';';';');';';';';';';';'; –