TestCafe:如何获取HTML标签外的时间戳内容?
解决TestCafe获取完整时间戳文本的问题
你当前的Selector选中的是<strong id="timestamp_label">元素,它的文本仅包含“Current Timestamp:”,而时间戳是其父div的直接文本节点(和strong同级),所以需要调整Selector到包含两者的父元素上。
方案1:直接选中父div元素
修改页面对象代码,将Selector指向包含完整内容的div:
import { Selector } from 'testcafe'; class page { lastUpdateTimeStamp = Selector('.div-group'); // 通过class定位父div } export default new page();
方案2:通过strong元素定位其父元素
如果不想依赖class,也可以通过目标strong元素的父元素来定位:
import { Selector } from 'testcafe'; class page { lastUpdateTimeStamp = Selector('#timestamp_label').parent(); } export default new page();
获取完整文本
在测试中获取该元素的文本内容即可得到完整结果:
test('获取完整时间戳文本', async t => { const fullTimestampText = await page.lastUpdateTimeStamp.textContent; // fullTimestampText 会包含 "Current Timestamp: 12/13/2023 10:55 A.M. EST" });
内容的提问来源于stack exchange,提问作者Ivonne Perez
相关产品推荐
相关产品推荐

