我是 TestCafe 的新手。
下面是示例 HTML 代码,当我创建带有 id 的选择器时,它仅返回“当前时间戳:”,并且我需要当前时间的时间戳。
Page object file:
import { Selector } from 'testcafe';
class page {
lastUpdateTimeStamp = Selector('#timestamp_label');
export default new page();
<div _ngcontent-ww-123="" class="div-group">
<strong _ngcontent-ww-123="" id="timestamp_label">Current Timestamp:</strong>
12/13/2023 10:55 A.M. EST
</div>
尝试使用下面的选择器:
lastUpdateTimeStamp = Selector('#timestamp_label');
Returned value of only text "Current Timestamp:"
Expecting Current Timestamp: 12/13/2023 10:55 A.M. EST
出现此问题的原因在于您创建选择器的方式。
lastUpdateTimeStamp = Selector('#timestamp_label');
在您的示例中,您仅选择“strong”html 元素。但是,为了选择“当前时间戳:12/13/2023 10:55 A.M.” EST',您需要选择作为两个文本的父元素的元素。在示例中,它是 class="div-group" div。因此,以下选择器应该可以解决该问题:
lastUpdateTimeStamp = Selector('.div-group');