Testcafe 从元素中获取文本

Testcafe get text from element

我正在尝试从 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);

但是我不能把那个内部文本放到一个变量中,例如。我尝试使用来自

的 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

并尝试按照 建议

使用普通选择器
const text = Selector('.my-form > a').innerText;
const inner = await text.textContent;
console.log(inner);

// prints: undefined

如何从元素中提取文本?我了解 t.selectText 在这种情况下受到限制,对吧?

来自documentation你想要的:

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

那就行了:

const paragraph      = Selector("p").withText("possible entries");
const extractEntries = await paragraph.textContent;