gpt4 book ai didi

javascript - 使用 Selenium 测试 Polymer 元素和阴影 DOM

转载 作者:搜寻专家 更新时间:2023-11-01 05:20:48 29 4
gpt4 key购买 nike

我在将 Selenium 与影子 DOM 内的元素一起使用时遇到问题。这有什么诀窍吗?我做错了什么吗?

这是我的各种尝试:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().forBrowser('chrome').build();

driver.get('https://shop.polymer-project.org/');

// Goal is to find shop-app #shadow-root app-header
//

// This is OK; no shadow DOMs
driver.findElement(webdriver.By.css('shop-app'));

// This fails because:
// NoSuchElementError: no such element: Unable to locate element
driver.findElement(webdriver.By.css('shop-app /deep/ app-header'));

// This fails because:
// NoSuchElementError: no such element: Unable to locate element
driver.findElement(webdriver.By.css('shop-app::shadow app-header'));

// This fails because:
// TypeError: Custom locator did not return a WebElement
driver.findElement(webdriver.By.js(function() {
return document.querySelector('shop-app /deep/ app-header');
}));

// This fails because:
// TypeError: Custom locator did not return a WebElement
driver.findElement(webdriver.By.js(function() {
return document.querySelector('shop-app::shadow app-header');
}));

// This fails because:
// WebDriverError: unknown error: Cannot read property 'querySelector' of null
driver.findElement(webdriver.By.js(function() {
return document.querySelector('shop-app').shadowRoot.querySelector('app-header');
}));

// This fails because:
// WebDriverError: unknown error: Cannot read property 'header' of undefined
driver.findElement(webdriver.By.js(function() {
return document.querySelector('shop-app').$.header;
}));

我正在运行节点 7.1.0 和 selenium-webdriver 3.0.1。

最佳答案

我认为最好使用 selenium 选择器并注入(inject)脚本以获取影子根:

def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root

outer = expand_shadow_element(driver.find_element_by_css_selector("#test_button"))
inner = outer.find_element_by_id("inner_button")
inner.click()

为了更好地理解这一点,我刚刚在 Chrome 的下载页面中添加了一个可测试的示例,单击搜索按钮需要打开 3 个嵌套的影子根元素: enter image description here

import selenium
from selenium import webdriver
driver = webdriver.Chrome()


def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root

driver.get("chrome://downloads")
root1 = driver.find_element_by_tag_name('downloads-manager')
shadow_root1 = expand_shadow_element(root1)

root2 = shadow_root1.find_element_by_css_selector('downloads-toolbar')
shadow_root2 = expand_shadow_element(root2)

root3 = shadow_root2.find_element_by_css_selector('cr-search-field')
shadow_root3 = expand_shadow_element(root3)

search_button = shadow_root3.find_element_by_css_selector("#search-button")
search_button.click()

采用其他答案中建议的相同方法有一个缺点,即它对查询进行硬编码,可读性较差,并且您不能将中间选择用于其他操作:

search_button = driver.execute_script('return document.querySelector("downloads-manager").shadowRoot.querySelector("downloads-toolbar").shadowRoot.querySelector("cr-search-field").shadowRoot.querySelector("#search-button")')
search_button.click()

关于javascript - 使用 Selenium 测试 Polymer 元素和阴影 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758283/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com