gpt4 book ai didi

javascript - Nightmare 测试 click() 选择器

转载 作者:行者123 更新时间:2023-11-30 12:15:02 24 4
gpt4 key购买 nike

我想获取对 DOM 元素的引用并单击它,但我不知道如何找到它。在 DOM 中将像:

$('.filter-buttons :nth-child(2)').get(0).click() , or using [0]instead of get(0)

但是对于 Nightmare 函数,click 只接受一个参数 .click(selector)

.click('.filter-buttons :nth-child(2)')

.问题是我不知道把 get(0) 放在哪里。有什么想法吗?

最佳答案

正如它所提到的,.get(0) 可以让您找到一个 dom 节点,并且您正在应用一个 dom click 事件。所以我建议你使用 document.querySelector() 方法:

.click(document.querySelector('.filter-buttons :nth-child(2)'))

document.querySelector() :

这个方法只返回一个dom节点。这正是您的要求。

来自文档:

Returns the first element within the document (using depth-first pre-order traversal of the document's nodes|by first element in document markup and iterating through sequential nodes by order of amount of child nodes) that matches the specified group of selectors.

Example

In this example, the first element in the document with the class "myclass" is returned:

var el = document.querySelector(".myclass");

关于javascript - Nightmare 测试 click() 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32625206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com