Testcafe 从元素中获取文本 [英] Testcafe get text from element

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

问题描述

我正在尝试从 Chrome 上的模式中获取文本.使用控制台,我可以得到如下内部文本:

I'm trying to get a text from a modal on Chrome. Using the console, I can get the inner text as follows:

document.querySelector('.my-form > a').innerText
// returns http://a-url.com

现在,在我的测试中,我可以使用评估元素

Now, on my test, I can evaluate the element using

const myText = Selector('.my-form > a').innerText;
await t
  .expect(myText).contains('url');

我什至可以点击那个网址

and I can even click on that URL

await t.click(myText);

但例如,我不能将该内部文本放入变量中.我尝试使用 这篇文章

but I cannot put that inner text to a variable, for instance. I tried using a ClientFunction from this post

const getUrl = ClientFunction(() => document.querySelector('.my-form > a').innerText);

test('My Test', async t => {
const text = await getUrl();
console.log(text);
});

// this results in 
// TypeError: Cannot read property 'innerText' of null

并尝试使用普通选择器作为 这篇文章建议

and tried using a plain Selector as this post suggests

const text = Selector('.my-form > a').innerText;
const inner = await text.textContent;
console.log(inner);

// prints: undefined

如何从元素中提取文本?我知道 t.selectText 在这种情况下是有限的,对吧?

How to extract a text from an element? I understand that t.selectText is limited in this scenario, right?

推荐答案

来自 文档 你想要的:

const text = await Selector('.my-form > a').innerText;

这篇关于Testcafe 从元素中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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