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

时间:2017-01-20 06:24:11

标签: angularjs selenium selenium-webdriver javascript-events

我们如何使用selenium webdriver访问psuedo html元素?示例input :: after,input :: before等。这些元素内容不以dom显示,但在页面上可见。

1 个答案:

答案 0 :(得分:1)

假设我们有以下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>

要获取content:before伪元素,您可以使用JavaScript代码中插入的Selenium

<强>的Python

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

<强>爪哇

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

返回值:"Read this -"