我正在使用Selenium用Javascript编写测试自动化。尝试提取DOM元素的类属性对我来说不起作用。这是我的代码:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.ie()).
build();
var usernameField = driver.findElement(webdriver.By.id('username'));
var classes = usernameField.getAttribute('class');
console.log(classes);
这将打印以下内容:
{ then: [Function: then],
cancel: [Function: cancel],
isPending: [Function: isPending] }
请说明如何查找元素的属性值。
答案 0 :(得分:3)
发现问题,在分配任何值之前,异步触发了console.log()。使用then语句强制它按顺序执行修复了问题。
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.ie()).
build();
var usernameField = driver.findElement(webdriver.By.id('username'));
usernameField.getAttribute('class')
.then(function(classes){
console.log(classes);
});