WebdriverWait 显示 TimeoutException,如果我使用 sleep.time 它工作正常
WebdriverWait is showing TimeoutException, if I use sleep.time it works ok
我想在 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:
如果我使用 time.sleep(10)
它工作正常并点击元素。我已经将所有链接恢复到 time.sleep,直到我可以让 WebdriverWait
正常工作。
我的 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)
导入的是:
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()
我的 WebdriverWait
语法正确吗?为什么会出现 TimeoutException?
如果我使用 time.sleep(secs)
,它工作正常但不是最有效的方法。
您没有正确使用显式等待 - 您需要使用预期条件 - 在 return True
之前会重复调用的可调用对象。您正在 returning click()
方法的结果 returns None
这是虚假的 - 预期条件永远不会 returns True
并且,因此,您得到 TimeoutException
.
在这种情况下,内置 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()
wait = WebDriverWait(driver, 10)
paragraph = wait.until(EC.element_to_be_located((By.CSS_SELECTOR,"body > p:nth-child(3)")))
paragraph.getText()
我想在 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:
如果我使用 time.sleep(10)
它工作正常并点击元素。我已经将所有链接恢复到 time.sleep,直到我可以让 WebdriverWait
正常工作。
我的 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)
导入的是:
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()
我的 WebdriverWait
语法正确吗?为什么会出现 TimeoutException?
如果我使用 time.sleep(secs)
,它工作正常但不是最有效的方法。
您没有正确使用显式等待 - 您需要使用预期条件 - 在 return True
之前会重复调用的可调用对象。您正在 returning click()
方法的结果 returns None
这是虚假的 - 预期条件永远不会 returns True
并且,因此,您得到 TimeoutException
.
在这种情况下,内置 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()
wait = WebDriverWait(driver, 10)
paragraph = wait.until(EC.element_to_be_located((By.CSS_SELECTOR,"body > p:nth-child(3)")))
paragraph.getText()