Selenium用javascript获取元素的class属性 [英] Selenium get class attribute of element with javascript

查看:1913
本文介绍了Selenium用javascript获取元素的class属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium用Javascript编写测试自动化。尝试提取DOM元素的类属性对我来说不起作用。这是我的代码:

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

这将打印以下内容:

{ then: [Function: then],
cancel: [Function: cancel],
isPending: [Function: isPending] }

请说明如何查找元素的属性值。

Please indicate how to find the attribute values of the element.

推荐答案

发现问题,在分配任何值之前异步触发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);
 });

这篇关于Selenium用javascript获取元素的class属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆