且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Selenium用javascript获取元素的class属性

更新时间:2023-12-05 12:46:46

发现问题,在分配任何值之前异步触发console.log()。强制它使用then语句顺序执行修复了问题。

Found the issue, console.log() was being fired asynchronously before any values were assigned. Forcing it to execute sequentially using then statement fixed the problem.

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);
 });