【问题标题】:Python + Selenium: Reload a page after a timeout on WebDriverWait callPython + Selenium:在 WebDriverWait 调用超时后重新加载页面
【发布时间】:2020-05-24 06:41:39
【问题描述】:

我正在寻找一种在 Python Selenium 中实现以下的正确方法

  1. 加载页面
  2. 等待一段时间(例如 30 秒)让按钮可点击(通过调用 WebDriverWait)
  3. 如果遇到 TimeoutException,请重新加载页面,即转到步骤 1)
url = 'https://...'
driver = webdriver.Chrome('./chromedriver')

try:
    driver.get(url)
    wait = WebDriverWait(driver, 30)
    element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'button')))
except TimeoutException as e: 
    <reload the url again>

【问题讨论】:

    标签: python selenium loops selenium-webdriver webdriverwait


    【解决方案1】:

    如果找不到元素,您可以创建自动调用刷新的函数。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import os
    
    driver = webdriver.Chrome(executable_path =os.path.abspath(os.getcwd()) + "/chromedriver")
    driver.get("https://selenium-python.readthedocs.io/waits.html")
    
    
    def refresh():
        try:
            element = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CLASS_NAME, "button"))
            )
        except:
            driver.refresh()
            refresh()
    
    
    refresh()
    

    【讨论】:

      【解决方案2】:

      您可以获取带有explicit wait 的元素列表,其中在Dom 中存在buttonclass name 的元素。如果元素列表为空,则可以刷新页面。

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      url = 'https://...'
      driver = webdriver.Chrome('./chromedriver')
      driver.get(url)
      wait = WebDriverWait(driver, 30)
      
      if  len(wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME,'button'))))==0 :
        driver.refresh()
      

      【讨论】:

        【解决方案3】:

        执行以下任务:

        1. 加载页面
        2. 等待一段时间(例如 30 秒)让按钮可点击(通过调用 WebDriverWait)
        3. 如果遇到 TimeoutException,请重新加载页面,即转到步骤 1)

        您可以使用以下Locator Strategy。为了演示,我将考虑一个在Google Search Home Page 上不可用的元素:

        • 代码块:

          from selenium import webdriver
          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.common.by import By
          from selenium.webdriver.support import expected_conditions as EC
          
          options = webdriver.ChromeOptions() 
          options.add_argument("start-maximized")
          options.add_experimental_option("excludeSwitches", ["enable-automation"])
          options.add_experimental_option('useAutomationExtension', False)
          driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
          while True:
              try:
                  driver.get("https://www.google.com/")
                  WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "kokei")))
                  print("Button found")
                  break
              except TimeoutException:
                  print("Button not found ... reloading page")
                  continue
          # perform your remaining steps here on successfully finding the clickable element
          driver.quit()
          
        • 控制台输出:

          Button not found ... reloading page
          Button not found ... reloading page
          Button not found ... reloading page
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多