【问题标题】:Function not waiting for intended period of time - Python Selenium函数未等待预期的时间段 - Python Selenium
【发布时间】:2018-12-25 00:37:11
【问题描述】:

我有一个函数可以根据 xpath 返回元素。

DEFAULT_WAIT_TIME_SEC = 10
def wait_for_element_by_xpath(self, xpath,
                                  wait_time=DEFAULT_WAIT_TIME_SEC):
        print ("wait_time")
        print (wait_time)
        result = self.driver.find_element(By.XPATH, xpath)
        wait = WebDriverWait(self.driver, wait_time)
        wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
        return self.driver.find_element(By.XPATH, xpath)

我故意将默认超时时间设置为 10 秒。

        timeValue = "//span[contains(text(),'online Time')]"
        self.wait_for_element_by_xpath(timeValue, 70)

我已经过了 70 秒来等待元素出现在屏幕上。但是当我运行测试时,它只等待 10 秒并抛出 NoSuchElementException。

我故意在我的函数中打印 wait_time 以检查它选择的时间、默认时间或我的给定时间(70 秒)。

wait_time 打印 70。

那么为什么它在 10 秒后抛出异常而不是等待预期的 70 秒?

【问题讨论】:

    标签: python python-3.x selenium-webdriver automation webdriver


    【解决方案1】:

    您应该从函数定义中删除 result = self.driver.find_element(By.XPATH, xpath) 行。试试下面的代码:

    DEFAULT_WAIT_TIME_SEC = 10
    def wait_for_element_by_xpath(self, xpath,
                                      wait_time=DEFAULT_WAIT_TIME_SEC):
            print ("wait_time is %s" % wait_time)
            wait = WebDriverWait(self.driver, wait_time)
            element = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
            return element
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2019-06-22
      • 2021-08-20
      • 1970-01-01
      相关资源
      最近更新 更多