Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath to find pseudo-element ::after in side a div element with out any content

I am trying to write xpath to find a checkbox selected or not, this checkbox is being changed using css ::after element. Below are two elements I have

//div[@class='FormBlock-formItem2' and .//text()='Scoped In']//div[@class='FormBlock-controlIndicator']

two elements for checkboxes

I need to find that 'Scoped Out' is not selected and 'Scoped In' is selected. The only difference I see is '::after' in the second section of checkbox. I tried the xpath :" //div[@class='FormBlock-formItem2' and .//text()='Scoped In']//div[@class='FormBlock-controlIndicator'] " but this is finding the 'Scoped In' element but I am not able to verify if its checked or not. Struggling from couple of days. Kindly help.

like image 633
Naveen kumar Avatar asked Aug 23 '18 18:08

Naveen kumar


People also ask

What is :: before in xpath?

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

How do I find the xpath of a div?

We can find an element using the xpath locator with Selenium webdriver. To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.

How do I find after 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.

How do I get after xpath?

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


2 Answers

Unfortunately, that's not possible with XPath. As mentioned by Tomalak, Pseudo-elements don't exist in the DOM tree (hence the name), therefore they cannot be selected with XPath and Selenium does not expose them as well. In general, ::before and ::after pseudo-elements are used for styling of containing element.

If you want to check whether ::before or ::after pseudo-elements are present or what's their content you can use a CSS selector, like this:

console.log(window.window.getComputedStyle(
	document.querySelector('#item'), ':begin'
));
//or
window.getComputedStyle(
	document.querySelector('#item'), ':after'
).getPropertyValue('color');
#item::after {
    content: 'checked';
    color: rgb(255, 0, 0);
}
<div id="item">
</div>

And then use a JavascriptExecutor to inject the JS into the browser and get the return value:

JavascriptExecutor js = (JavascriptExecutor) driver;         
js.executeScript("return document.title");
like image 31
wp78de Avatar answered Sep 19 '22 15:09

wp78de


Check code below if return correct true/false:

driver.findElement(By.xpath("//label[./div[@class='FormBlock-itemText' and .='Scope In']]/input")).isSelected();
like image 146
Sers Avatar answered Sep 17 '22 15:09

Sers