2017-03-18 29 views
0

我一直在遇到一個問題,試圖使用JavaScript selenium-webdriver從WebElement中提取純文本。我目前正在使用MicrosoftEdge瀏覽器和驅動程序,所有設置都正確,但我無法弄清楚爲什麼試圖從WebElement獲取值在JavaScript Selenium中被破壞了。例如,假設下列文件:Selenium(JS)中WebElement的getText()失敗

<html> 
    <body> 
     <p>Hello</p> 
     <a href="http://bing.com/maps" class="myClass">Maps</a> 
    </body> 
</html> 

和隨後的查詢(設置中省略爲了簡潔):

browser.get("file:///path/to/index.html"); 
browser.sleep(1000); //Just to be sure... 
var ep = browser.findElement(By.linkText("Maps")); 
ep.then(elm => console.log(elm.getText())); //Doesn't work! 
ep.then(elm => console.log(elm.getAttribute("innerHtml"))); //Doesn't work! 
ep.then(elm => elm.click()); //works 

凡是不直接與所述元件相互作用(例如的SendKeys或點擊),包括像isDisplayed()這樣的調用似乎失敗並在控制檯中提供一些異常跟蹤。我在Windows 10上使用selenium-webdrivier 3.3.0和nodejs版本6.10.0。

API爲便利。

回答

1

getText和getAttribute返回承諾,用請求的值解析。

elm.getText().then(function (text) { 
    console.log(text); 
});