1

I would like to retrieve parent web element of pseudo element (If it could be named parent), but as far as I know selenium's methods of searching for web elements are not suitable for searching pseudo elements.
However, JavaScript manipulates pseudo elements freely, so I would like to ask, if there is a method that could return css selector/xpath of parent web element for pseudo element (To be more precise "::after") in JavaScript.
In other words, I've got this:

<html>
<body>
<label id="parent">
<span> Some text </span>
::after
</label>
</body>
</html>

And I would like to get that:"#parent"

Thank you in advance.

Kiseki
  • 37
  • 7

2 Answers2

0

If you need to select parent based on text of span, then you can use something like this :

driver.findElement(By.xpath("//label[.//span[text()='Some Text']]"));

The above code will help you find that label which is followed by a span with text = "Some Text".

If you are not sure of the tag and you are sure of getting the results using javascript. Try executing the JavaScript Code in selenium using the below syntax: (Python)

driver.execute_script("document.querySelector('#item'), ':after'")

Update the script as per your requirement in your case to find the parent also.

Let me know in comments below if this doesnt help you.

Shruti Agarwal
  • 887
  • 2
  • 14
  • 29
  • The point is, that the "::after" is not a part of element :) It is an independent element, which could be located in a various lot of places (In my case in only one) – Kiseki May 22 '17 at 15:39
  • I mean ::after element could be set after any tag, not necessarily after , and I would like to retrieve position of (possibly every) ::after element, OR to check whether certain Web Elements has ::after pseudo element as a child – Kiseki May 23 '17 at 10:08
  • If you can find it using javascript execute the JS code in selenium.Please find it updated in my answer – Shruti Agarwal May 23 '17 at 11:17
0

Find child element first and add context node to the child element, like this:

WebElement child = driver.findElement(By.xpath("xpath of child"));
WebElement parent = child.findElement(By.xpath("./.."));
Sai G
  • 74
  • 2