Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Selenium get class attribute of element with javascript

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Selenium get class attribute of element with javascript

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×