javascript - Nightmare JS 点击集合中的随机链接
问题描述
我可以使用以下方法单击链接:
nightmare.click("#video-title")
但是,它会单击具有该 ID 的第一个元素。当我
document.querySelectorAll('[id=video-title]')
在 Youtube 上进行搜索时,我得到了大约 85 个结果。有没有办法使用 Nightmare JS 随机点击一个?在通话中返回该列表evaluate
对我不起作用
.evaluate(() => {
return Array.from(document.querySelectorAll('[id=video-title]'))
})
.then(videoLinks => {
nightmare.click(videoLinks[0])
})
Nightmare 在点击时给出了这个错误:
Error: Failed to execute 'querySelector' on 'Document': '[object Object]' is not a valid selector.
解决方案
像这样的东西应该工作:
.evaluate(() => {
const list = document.querySelectorAll('[id=video-title]')
return list[Math.floor(Math.random()*list.length)]
})
作为回报,它通过 queryselectorAll 为您提供 NodeList 中的随机元素
推荐阅读
- drupal - Drupal 8 - 作曲家依赖问题
- angular - 如何在通用 Angular 和 nginx 中启用 gzip 文本压缩?
- matrix - 带有手动添加列的 SSRS 排序矩阵
- iccube - 使用 Chrome Headless 进行批量打印 - 创建 PDF 报告后进程未终止
- c# - 强制 IConfigurationBuilder.Bind() 在绑定对象失败时抛出错误
- macros - 如何在没有管理员权限的情况下安装/运行 autohotkey?
- apache-spark - 如何在 Azure Databricks 中写入 HBase?
- r - 完美对齐直方图下的水平箱线图
- java - 如何从 BroadcastReceiver 打开 Wifi 设置
- python - 在 Python 中使用 attr 模块和元类