【问题标题】:Selenium closing webdriver during for loop在 for 循环期间 Selenium 关闭 webdriver
【发布时间】:2022-01-16 01:43:33
【问题描述】:

这里是硒的新手。如果这个问题的答案就在我面前,我提前道歉,但我似乎无法弄清楚这一点。

我有一个用于网页抓取脚本的 for 循环,它运行良好。然而,在执行过程中,webdriver 关闭了窗口,代码在没有打开 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!`);
  }
}

}

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 您的日志中有错误吗?你在窗户上吗?换个浏览器试试。
  • 好的,我收到一条错误消息,上面写着:NoSuchSessionError: invalid session id }

标签: javascript node.js selenium selenium-chromedriver


【解决方案1】:

事实证明,我的互联网断开连接,这就是导致此问题的原因。我只是在超时 5 分钟后在 catch 语句中添加了一个重建语句,就解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多