简体   繁体   English

我们如何使用 selenium webdriver 访问伪 html 元素?

[英]How can we access pseudo html elements using selenium webdriver?

How can we access pseudo html elements using selenium webdriver?我们如何使用 selenium webdriver 访问伪 html 元素? Example input::after , input::before etc. These elements contents are not displayed in dom but is visible on page.例如input::afterinput::before等。这些元素的内容不会显示在 dom 中,但在页面上是可见的。

Lets say we have following HTML structure (borrowed from w3schools ):假设我们有以下HTML结构(从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:要获取:before伪元素的content ,您可以使用以下插入Selenium代码的JavaScript

Python 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 -"返回值: "Read this -"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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