Selenium get class attribute of element with javascript
Problem
I am using using Selenium to write test automation with Javascript. Trying to extract class attributes of a DOM Element does not work for me. Here is my code:
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);
This prints the following:
{ then: [Function: then],
cancel: [Function: cancel],
isPending: [Function: isPending] }
Please indicate how to find the attribute values of the element.
Problem courtesy of: Lilit Yenokyan
Solution
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);
});
Solution courtesy of: Lilit Yenokyan
Discussion
View additional discussion.