【问题标题】:How i can get text from web element and print in console (for example)我如何从 Web 元素中获取文本并在控制台中打印(例如)
【发布时间】:2019-01-29 07:25:24
【问题描述】:

我在从网页上的元素中获取文本时遇到问题。 我正在使用 TestCafe e2e 框架,并希望将文本 Web 元素的内容打印到控制台。可以提供一些代码吗?

const getInnerText = ClientFunction(() => homePage.kzLink.innerText);
console.log(getInnerText());

我得到了什么:

ReExecutablePromise { _then: [], _fn: [Function], _taskPromise: null }

【问题讨论】:

    标签: javascript testing automated-tests e2e-testing testcafe


    【解决方案1】:

    要执行客户端函数,请使用 await 关键字和依赖项调用它。

    const getInnerText = ClientFunction(() => homePage.kzLink.innerText, { 
        dependencies: { homePage.kzLink }
    });
    
    test('My Test', async t => {
        const text = await getInnerText();
        console.log(text);
    });
    

    【讨论】:

    • 如果你添加依赖: const getInnerText = ClientFunction(() => homePage.kzLink.innerText, { dependencies: { homePage.kzLink } });
    • 如果homePage.kzLink 是一个选择器,你可以简单地这样做:console.log(await homePage.kzLink.textContent)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2018-04-28
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多