简体   繁体   中英

How can we access pseudo html elements using selenium webdriver?

How can we access pseudo html elements using selenium webdriver? Example input::after , input::before etc. These elements contents are not displayed in dom but is visible on page.

Lets say we have following HTML structure (borrowed from w3schools ):

<!DOCTYPE html>
<html>    
  <head>
    <style>
      p::before {
        content: "Read this -";}
    </style>
  </head>    
  <body contenteditable="false">   
    <p>My name is Donald</p>
    <p>I live in Ducksburg</p>
    <p><b>Note:</b> For this selector to work in IE8, a DOCTYPE must be declared, and you must use the old, single-colon CSS2 syntax (:before instead of ::before).</p>
  </body>    
</html>

To get content of :before pseudo-element you might use following JavaScript inserted in Selenium code:

Python

driver.execute_script("return window.getComputedStyle(document.querySelector('p'), ':before').getPropertyValue('content');")

Java

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("return window.getComputedStyle(document.querySelector('p'), ':before').getPropertyValue('content');");

Returned value: "Read this -"

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