Selenium get class attribute of element with javascript
12,102
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);
});
Author by
Lilit Yenokyan
Updated on August 02, 2022Comments
-
Lilit Yenokyan almost 2 years
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.