Selenium 在 for 循环期间关闭 webdriver
Selenium closing webdriver during for loop
这里是 selenium 的新手。如果这个问题的答案就在我面前,我提前道歉,但我似乎无法弄清楚这个问题。
我有一个用于网络抓取脚本的 for 循环,它工作正常。然而,在执行过程中,webdriver 关闭了 window 并且代码在没有 chrome 浏览器打开的情况下继续执行,这里是 for 循环:
for (let i = 1; i < 3000; i++) {
let continueCodeExecution = false;
//check for element with warning stating that id doesn't exist in db
//if try statement is successful, element doesn't exist in db
try {
await driver.get(`https://tbc.wowhead.com/spell=${i}`);
const notFoundElement = await driver.findElement(
By.xpath(
"//div[contains(text(), 'It may have been removed from the game.')]"
)
);
arrayOfFailedSpellIDs.push(i);
console.log(`Spell ID: ${i}, confirmed to not exist`);
} catch (error) {
//if in this catch statement, couldn't find the element, meaning id exists
continueCodeExecution = true;
console.log(`Spell ID: ${i} did not find notFoundElement.`);
}
//this only executes if the notFoundElement was failed to be found in DOM
if (continueCodeExecution === true) {
try {
const dataToPushToArray = await parseToolTipInOrder(i, driver);
arrayOfScrapedData.push(dataToPushToArray);
} catch (error) {
arrayOfPotentiallySkippedIDs.push(i);
console.log(`Spell ID: ${i}, Failed to scrape data!`);
}
}
}
关于如何解决这个问题有什么想法吗?
原来是我的网络断开了,这就是导致这个问题的原因。我只是在超时 5 分钟后向 catch 语句添加了一条重建语句,这就解决了问题。
这里是 selenium 的新手。如果这个问题的答案就在我面前,我提前道歉,但我似乎无法弄清楚这个问题。
我有一个用于网络抓取脚本的 for 循环,它工作正常。然而,在执行过程中,webdriver 关闭了 window 并且代码在没有 chrome 浏览器打开的情况下继续执行,这里是 for 循环:
for (let i = 1; i < 3000; i++) {
let continueCodeExecution = false;
//check for element with warning stating that id doesn't exist in db
//if try statement is successful, element doesn't exist in db
try {
await driver.get(`https://tbc.wowhead.com/spell=${i}`);
const notFoundElement = await driver.findElement(
By.xpath(
"//div[contains(text(), 'It may have been removed from the game.')]"
)
);
arrayOfFailedSpellIDs.push(i);
console.log(`Spell ID: ${i}, confirmed to not exist`);
} catch (error) {
//if in this catch statement, couldn't find the element, meaning id exists
continueCodeExecution = true;
console.log(`Spell ID: ${i} did not find notFoundElement.`);
}
//this only executes if the notFoundElement was failed to be found in DOM
if (continueCodeExecution === true) {
try {
const dataToPushToArray = await parseToolTipInOrder(i, driver);
arrayOfScrapedData.push(dataToPushToArray);
} catch (error) {
arrayOfPotentiallySkippedIDs.push(i);
console.log(`Spell ID: ${i}, Failed to scrape data!`);
}
}
}
关于如何解决这个问题有什么想法吗?
原来是我的网络断开了,这就是导致这个问题的原因。我只是在超时 5 分钟后向 catch 语句添加了一条重建语句,这就解决了问题。