Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use WebdriverWait for when clicking elements in Python Webdriver.

I get the following TimeoutException error when using WebdriverWait:

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 WebdriverWait to work properly.

My code snippet for WebdriverWait is:

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 WebdriverWait syntax correct? Why the TimeoutException?

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

like image 479
Riaz Ladhani Avatar asked Dec 01 '22 00:12

Riaz Ladhani


1 Answers

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 None which is falsy - the expected condition never returns True and, hence, you are getting TimeoutException.

In this case, built-in element_to_be_clickable fits nicely, example:

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()
like image 108
alecxe Avatar answered Dec 05 '22 13:12

alecxe