4

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Riaz Ladhani
  • 3,946
  • 15
  • 70
  • 127
  • Are you using time.sleep instead of the WebDriverWait? Is that the entire exception message? – Mike Weber May 19 '15 at 16:51
  • The exception was showing when i was using WebdriverWait. Then i reverted to using time.sleep until i fix the syntax for WebdriverWait. – Riaz Ladhani May 20 '15 at 08:19

2 Answers2

21

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()
Sean Breckenridge
  • 1,932
  • 16
  • 26
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • i have used something like `wait.until(EC.presence_of_element_located((By.LINK_TEXT, i)))` where i is a varibale but I am getting `TimeOutException`. Please point me out the mistake that I am doing here. – proprius Jan 16 '16 at 12:08
  • @proprius it heavily depends on a use case, please elaborate into a separate question if you need help and throw a link here. Thanks! – alecxe Jan 16 '16 at 15:26
  • @alecxe here is the link to the question: [link] (http://stackoverflow.com/questions/34827365/timeoutexception-when-use-link-text-with-explicit-wait-in-selenium-webdriver-wit/34836229#34836229) – proprius Jan 17 '16 at 16:01
  • @proprius thanks, saw it already, thanks. Please provide the HTML representation of the link and include it into that question. – alecxe Jan 17 '16 at 16:04
  • I have updated the question by adding the particular tag I was looking for in html code. – proprius Jan 18 '16 at 04:38
  • your answer is perfect – myworldbox Aug 04 '21 at 17:54
0
wait = WebDriverWait(driver, 10)
paragraph = wait.until(EC.element_to_be_located((By.CSS_SELECTOR,"body > p:nth-child(3)")))
paragraph.getText()
Maroun
  • 94,125
  • 30
  • 188
  • 241
gourav
  • 1
  • 1