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;

相关文章