Testcafe从元素中获取文本

问题描述 投票:3回答:1

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

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

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

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

我甚至可以点击该URL

await t.click(myText);

但是我不能把那个内部文本放到一个变量上。我尝试使用this post的ClientFunction

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

并尝试使用普通的选择器,如this post建议

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

// prints: undefined

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

javascript automated-tests e2e-testing web-testing testcafe
1个回答
4
投票

从你想要的documentation

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

© www.soinside.com 2019 - 2024. All rights reserved.