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']
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.
::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.
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.
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.
xpath("i[@class='tree-branch-head']::after")); System. out. println(plusOrMinusSign. getCssValue("content"));
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");
Check code below if return correct true/false:
driver.findElement(By.xpath("//label[./div[@class='FormBlock-itemText' and .='Scope In']]/input")).isSelected();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With