简体   繁体   English

NightwatchJS:如何循环使用document.querySelectorAll选择的元素?

[英]NightwatchJS: how to loop on elements selected using document.querySelectorAll?

I am trying to use document querySelectorAll to select an array of options. 我试图使用文档querySelectorAll选择一个选项数组。 I found out that we cant directly use document.querySelectorAll as it gives me error 我发现我们不能直接使用document.querySelectorAll因为它给了我错误

"ReferenceError: document is not defined" “ ReferenceError:未定义文档”

and instead use it with browser.execute as mentioned in this answer but still getting undefined. 而是按答案中所述将其与browser.execute一起使用,但仍未定义。 The console statement logs undefined, it should have given me an array of elements instead. console语句记录未定义的日志,它应该给了我一组元素数组。 If I run this css selector in developer tools, it gives me the array I expect but not here. 如果我在开发人员工具中运行此CSS选择器,它将为我提供期望的数组,但不在这里。 Any idea if I am doing something wrong? 知道我做错了什么吗?

browser.execute(function (data) {
   console.log(document.querySelectorAll('div#question_' + quesNo + ' .answerBlock .answer-value'));
   return true;
},[],null);

It may be due to the way you pass arguments to the browser.execute . 这可能是由于您将参数传递给browser.execute Try the following: 请尝试以下操作:

browser.execute((data) =>
{
    console.log(document.querySelectorAll('div#question_' + arguments[0] + ' .answerBlock .answer-value'));
    return true;
}, [quesNo]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM