我们如何使用selenium webdriver访问psuedo html元素?示例input :: after,input :: before等。这些元素内容不以dom显示,但在页面上可见。
答案 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 -"