Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting content in :after using XPath

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"));

However, I get an error: The string 'i[@class='tree-branch-head']::after' is not a valid XPath expression.

It seem the "::after" is not recognised.

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"?

like image 877
Rico Strydom Avatar asked Nov 30 '16 10:11

Rico Strydom


People also ask

How do I get after XPath?

xpath("i[@class='tree-branch-head']::after")); System. out. println(plusOrMinusSign. getCssValue("content"));

What is :: before in XPath?

::before. ::before is a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is.

Where is after element in selenium?

Selenium will not return to us the text content inside the before pseud-element. Trying to locate the ::after pseudo-element by using a locator like “. okButton::after” or “. okButton:after” you will get a NoSuchElementException and still come up empty-handed.

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.


2 Answers

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).

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.

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.

like image 146
gioele Avatar answered Oct 02 '22 12:10

gioele


Handle pseudo element in 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

like image 21
Nitin Singh Avatar answered Oct 02 '22 12:10

Nitin Singh