Reputation: 33361
In my Java Selenium automation project I have to know about some web elements if they are containing pseudo elements or not.
I already saw this question and the answer there simply doesn't work.
This looks better, however in my situation I already have a web element and I need to get it's content while in that answer the JavaScript receives a locator.
I absolutely do not familiar with JavaScript that's why I'm asking:
So, given a Selenium WebElement element. How can I know does this element contain pseudo element or not?
On a screenshot there is an element with red dot - the ::after
pseudo element while other elements doesn't have such red dot.
This is what I got from the dev tools as outerHTML for element containing the after
pseudo element. i even can't see it there and it is just similar to other elements there who doesn't contain pseudo elements.
<div workspaceid="49426bdc-59fe-44ec-82ea-8f567407c04f" data-test-id="9B1qjz0kWcfO" class="list-item ListItem__ListItemComponent-bsdtqz-0 dgogUS RoleItem-sc-1n184cw-0 jyfJwu role-item"><p class="text-component Texts__GenericText-sc-1dju4ks-0 czkGYX name p">9B1qjz0kWcfO </p></div>
Upvotes: 0
Views: 439
Reputation: 12255
You can use a WebElement
as an argument instead of locator in the JavascriptExecutor. Using getComputedStyle you can get all css properties for the pseudo-element
. Check CSS in devtools, find the red dot css property and then you can check it.
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
// JavaScript to get any css property include pseudo-element.
// Arguments is an array of parameters passed to the executeScript method (standart Selenium functionality)
String js = "return window.getComputedStyle(arguments[0], arguments[1]).getPropertyValue(arguments[2]);";
// Use WebElement instead of locator.
// Property "content" is an example of property, you should check css and get the red dot
String cssProperty = (String) jsExecutor.executeScript(js, element, ":after", "content");
// Check for the red dot css property
Upvotes: 1