我尝试了以下 selenium-webdriverJS 代码:
var webdriver = require('selenium-webdriver');
var browser = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
browser.get('http://localhost:1091/WebTours/sample.html');
var btn = browser.findElement(webdriver.By.id('show-coordinates'));
browser.sleep(3000);
var ids = btn.getAttribute("id");
console.log("attributes: " + ids); //expecting to run after above lines.
browser.quit();
预期的:导航到给定的 URL,找到元素,然后打印属性id
如下:
attributes: show-coordinates
Actual:在导航到 URL 本身之前,打印attributes:
并附有以下消息:
attributes: ManagedPromise::32 {[[PromiseStatus]]: "pending"}
环境:
Windows 7 - 64 bit
selenium-webdriver (installed using `npm install selenium-webdriver`)
ChromeDriver
Chrome
您需要使用以下方法从返回的承诺中提取值then();
所有 webdriver 命令都会返回一个承诺作为承诺管理器的一部分。这使您能够编写
driver.findElement(By.css('#searchBar')).clear();
driver.findElement(By.css('#searchBar')).sendKeys('hello');
driver.findElement(By.css('#searchButton')).click();
不必像这样链接它们:
driver.findElement(By.css('#searchBar')).clear().then(function() {
driver.findElement(By.css('#searchBar')).sendKeys('hello').then(function(){
driver.findElement(By.css('#searchButton')).click();
});
})
But getAttribute()
与许多 Webdriver JS 命令一样,返回一个值。在这种情况下,您需要注册一个承诺回调来提取该值。所以你的代码变成:
browser.get('http://localhost:1091/WebTours/sample.html');
var btn = browser.findElement(webdriver.By.id('show-coordinates'));
browser.sleep(3000);
var ids = btn.getAttribute("id").then(function(promiseResult){
console.log("attribute is: " + promiseResult);
});
browser.quit();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)