Javascript 正在尝试从selenium webdriver中的getAttribute()获取字符串值

Javascript 正在尝试从selenium webdriver中的getAttribute()获取字符串值,javascript,node.js,selenium,selenium-webdriver,chai,Javascript,Node.js,Selenium,Selenium Webdriver,Chai,我试图获取元素属性数据id的字符串值,并将其存储到变量中 因此,我可以检查以确保数据id与将在下一页加载的id相同 **这是我的密码: var dataId; it('Should redirect to request details page, with the selected contract', function(done) { driver.wait(until.elementLocated(by.xpath('//*[@id="contracts-list-panel"]

我试图获取元素属性
数据id
的字符串值,并将其存储到变量中

因此,我可以检查以确保
数据id
与将在下一页加载的id相同

**这是我的密码:

var dataId;

it('Should redirect to request details page, with the selected contract', function(done) {
    driver.wait(until.elementLocated(by.xpath('//*[@id="contracts-list-panel"]/div[2]/div'))).click();
    var element = driver.findElement(by.xpath('//*[@id="select-contract-modal"]/div[1]/div[2]/button'));
    dataId = element.getAttribute("data-id").then(function(attr) {
        expect(typeof attr).to.be.a("string");
        console.log(dataId.toString());
        element.click().then(function() {
            driver.wait(until.elementLocated(by.id('request-new-signer-email'))).then(done());
        });
    });
});   
console.log
返回以下内容:

ManagedPromise::1356 {[[PromiseStatus]]: "pending"}

你得到的是一个承诺,需要等待它的解决。您可以使用
.then()
语法,或者我不知道您为什么希望dataId在该范围内可用。在返回承诺“element.getAttribute(“data id”).then”之前,它不会被赋值,并且该承诺不会解析为值either@AndrewLively谢谢我为这个挣扎了好几个小时。我没有意识到getAttribute返回了一个承诺,因为我使用的是async/await,所以我必须等待结果,即
const bodyClass=await body.getAttribute('class')您将收到一个承诺,需要等待它得到解决。您可以使用
.then()
语法,或者我不知道您为什么希望dataId在该范围内可用。在返回承诺“element.getAttribute(“data id”).then”之前,它不会被赋值,并且该承诺不会解析为值either@AndrewLively谢谢我为这个挣扎了好几个小时。我没有意识到getAttribute返回了一个承诺,因为我使用的是async/await,所以我必须等待结果,即
const bodyClass=await body.getAttribute('class')