4

I'm getting TimeoutExceptions while clicking an element intermittently. I have tried with explicit waits and time.sleep(). It works for a while and again i get exceptions.

I want to understand if it is caused by expected condition.

WebDriverWait(self.driver, 40).until(EC.element_to_be_clickable((By.XPATH, <locator>  ))).click()

Will it help to avoid timeoutexception if I use below condition?

element = WebDriverWait(self.driver, 40).until(EC.presence_of_element_located((By.XPATH, <locator>  )))
element.click()
Jasmitha Meka
  • 1,387
  • 3
  • 10
  • 11
  • Can you explain a bit more? Are you getting the same timeout exception in both the explicit wait conditions? – demouser123 Apr 29 '20 at 04:24
  • 1
    `presence_of_element_located` checks if element is present is present in DOM. Not necessary that element is visible. Refer to this - https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html – Grasshopper Apr 29 '20 at 05:20
  • 1
    Will it be helpful to replace ```element_to_be_clickable``` with ```presence_of_element_located.``` to avoid timeoutexceptions? – Jasmitha Meka Apr 29 '20 at 06:43
  • 1
    Nope... it should work with `element_to_be_clickable`. If not then, the root cause is something else. – Grasshopper Apr 29 '20 at 09:18

1 Answers1

7

Based on official documentation from selenium and code implementation.

presence_of_element_located(locator) definition:

An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. locator - used to find the element returns the WebElement once it is located

element_to_be_clickable(locator):

An Expectation for checking an element is visible and enabled such that you can click it.

Expected conditions executes the condition in a loop for a defined timeout period. It will poll until true is returned for the condition. If false is returned, it will continue the loop until maximum timeout is reached.

JeffC
  • 22,180
  • 5
  • 32
  • 55
Infern0
  • 2,565
  • 1
  • 8
  • 21