2 回答
![?](http://img1.sycdn.imooc.com/5333a1bc00014e8302000200-100-100.jpg)
TA贡献1827条经验 获得超9个赞
您的代码为它找到的每个元素写入一个字符串值。您需要做的是获取这些字符串值并将它们添加到数组中,然后您可以从数组中获取随机条目:
let results = []; // <-- This is the array that the results will go into
this.elements('css selector', '#bfsDesktopFilters .search-filters__item #ddl-make > option', function (result) {
result.value.forEach(element => {
this.elementIdValue(element.ELEMENT, function (text) {
results.push(text.value); // Place individual result into array
});
});
console.log(results); // Log the finished array after loop is done
});
// Now that the array is populated with strings, you can get one random one out:
var rand = results[Math.floor(Math.random() * results.length)];
console.log(rand); // Log the random string
![?](http://img1.sycdn.imooc.com/5458626a0001503602200220-100-100.jpg)
TA贡献1836条经验 获得超4个赞
let result = this.elements('css selector', '#bfsDesktopFilters .search-filters__item #ddl-menter code hereake > option', function (result) {
return result.value.forEach(element => {
return this.elementIdValue(element.ELEMENT, function (text) {
return text.value;
})
})
})
var random = results[Math.floor(Math.random(`enter code here`) * results.length)];
console.log(random); // Log the random string
添加回答
举报