我正在等待驱动程序硒找到所有元素。 我使用“ await”,但代码无法正常工作。我在做什么错了?
这是我的代码:
const webdriver = require("selenium-webdriver"),
By = webdriver.By;
const chrome = require("chromedriver");
var driver = new webdriver.Builder() //driver provide interaction with hardware
.forBrowser("chrome") //mentioning the browser
.build(); // Opening our browser
driver.get("https://www.soccer24.com/");
setTimeout(main, 10 * 1000);
async function main() {
const listHome = await searchElements("team-home");
const listTime = await searchElements("time");
const listAway = await searchElements("team-away");
showLists(listTime, listHome, listAway);
}
async function searchElements(nameOfClass) {
const list = [];
await driver.findElements(By.className(nameOfClass)).then(function(elements) {
console.log("Elements");
console.log(elements.length);
for (var i = 0; i < elements.length; i++) {
elements[i].getText().then(function(text) {
list.push(text);
});
}
});
return list;
}
function showLists(listTime, listHome, listAway) {
console.log("Lista meczy\n");
for (let i = 0; i < listTime.length; i++) {
console.log(`${i + 1} ${listTime[i]} ${listHome[i]} ${listAway[i]} \n`);
}
}
函数showLists()处理空列表。