代码之家  ›  专栏  ›  技术社区  ›  SecretIndividual

显示更多使用硒需要睡眠

  •  0
  • SecretIndividual  · 技术社区  · 5 年前

    def load_more():
      while True:
        try:
          WebDriverWait(driver, 20, ignored_exceptions = (StaleElementReferenceException)).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".more"))).click()
          time.sleep(1)
        except TimeoutException:
          return
    

    上面的函数是在我加载初始页面后使用的,单击一个“showmore”按钮直到它不再存在。

    time.sleep(1) StaleElementReferenceException 在一个随机点。即使它被列在 ignored_exceptions .

    sleep() 功能?

    1 回复  |  直到 5 年前
        1
  •  1
  •   PDHide    5 年前

    https://www.selenium.dev/exceptions/

    https://developer.mozilla.org/en-US/docs/Web/WebDriver/Errors/StaleElementReference

    从文件中删除或文件已更改,则称为 陈腐

    一个例子是:

    A notification dialog that appears and dissappears in 3 second while you are checking for something else
    

    您的代码流可能如下所示:

     click something 
     do something else without thinking about the notification
    

    但它应该是这样的:

     click something
     wait for notification
     wait for notification to be removed
     do something