简体   繁体   中英

How do I find parents of pseudo element using selenium webdriver?

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.

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.

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("./..")); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM