2 回答

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

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
添加回答
舉報