2017-01-20 81 views
3

我在使用Selenium和陰影DOM內的元素時遇到了問題。這有什麼竅門嗎?難道我做錯了什麼?使用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和硒的webdriver 3.0.1。

回答

2

您應該使用driver.executeScript()方法。

然後使用「客戶端」Javascript訪問Shadow DOM。

請看another SO answer中的這個例子。

2

我認爲這是最好使用硒選擇和注入腳本只是採取的影子根:

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() 

做同樣的方法在其他的答案建議的缺點是它硬編碼的查詢,爲的可讀性和你CA沒有使用中間選擇的其他行動:

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() 
+1

其中一個最好的答案我見過SO(+1) – DebanjanB

相關問題