本文介绍了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 从元素中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)