Python WebdriverWait 显示 TimeoutException,如果我使用 sleep.time 它工作正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30331282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:16:12  来源:igfitidea点击:

WebdriverWait is showing TimeoutException, if i use sleep.time it works ok

pythonseleniumselenium-webdriverwebdriver

提问by Riaz Ladhani

I want to use WebdriverWait for when clicking elements in Python Webdriver. I get the following TimeoutException error when using WebdriverWait:

单击 Python Webdriver 中的元素时,我想使用 WebdriverWait。使用 WebdriverWait 时出现以下 TimeoutException 错误:

Traceback (most recent call last):
  File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore\TestCases\AdministrationPage_TestCase.py", line 30, in test_add_Project
    administration_page = login_page.clickAdministration()
  File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore\Pages\login.py", line 46, in clickAdministration
    WebDriverWait (self.driver, 10).until(lambda d: self.driver.find_element(*MainPageLocators.AdministrationButton_xpath).click())
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 75, in until
    raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message: 

If I use time.sleep(10)it works ok and clicks the elements. I have reverted all my links to time.sleep for now until I can get WebdriverWaitto work properly.

如果我使用time.sleep(10)它可以正常工作并单击元素。我已经将所有链接恢复到 time.sleep,直到我可以WebdriverWait正常工作为止。

My code snippet for WebdriverWait is:

我的 WebdriverWait 代码片段是:

class LoginPage(BasePage):

    #Click Administration from top menu
    def clickAdministration(self):
        WebDriverWait (self.driver, 10).until(lambda d: self.driver.find_element(*MainPageLocators.AdministrationButton_xpath).click())
        #time.sleep(10)
        return AdministrationPage(self.driver)

The imports are:

进口是:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException


class LoginPage_TestCase(unittest.TestCase):

     def test_add_Project(self):
        login_page = login.LoginPage(self.driver)
        login_page.userLogin_valid()
        administration_page = login_page.clickAdministration()

Is my WebdriverWaitsyntax correct? Why the TimeoutException?

我的WebdriverWait语法正确吗?为什么会出现超时异常?

If I use time.sleep(secs), it works fine but not the best efficient way to do it.

如果我使用time.sleep(secs),它工作正常,但不是最有效的方法。

采纳答案by alecxe

You are not using the Explicit Wait correctly - you need to make use of Expected Conditions - callables that would be called repeatedly until return True. You are returning the result of click()method which returns Nonewhich is falsy - the expected condition never returns Trueand, hence, you are getting TimeoutException.

您没有正确使用显式等待 - 您需要使用预期条件 - 可重复调用直到返回的可调用对象True。您正在返回返回错误的click()方法的结果None- 预期条件永远不会返回True,因此,您将获得TimeoutException.

In this case, built-in element_to_be_clickablefits nicely, example:

在这种情况下,内置非常element_to_be_clickable适合,例如:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(self.driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="test"]')))
element.click()

回答by gourav

wait = WebDriverWait(driver, 10)
paragraph = wait.until(EC.element_to_be_located((By.CSS_SELECTOR,"body > p:nth-child(3)")))
paragraph.getText()