presence_of_element_located()
presence_of_element_located()
is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. Hence the wait for the very first matched WebElement to be present.
Generic usecases
Generally, in your tests simply waiting for presence of element may not suffice to your requirement and you may need to wait for the element either to be visible or interactable.
To wait until the element is visible, you need to induce WebDriverWait for the visibility_of_element_located() and your effective code block will be:
return WebDriverWait(self.__driver, 10).until(
ec.visibility_of_element_located((By.XPATH, locator))
)
To wait until the element is interactable, you need to induce WebDriverWait for the element_to_be_clickable() and your effective code block will be:
return WebDriverWait(self.__driver, 10).until(
ec.element_to_be_clickable((By.XPATH, locator))
)
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
This usecase
In this usecase, the line:
ec.presence_of_element_located((By.XPATH, locator))
was unable to identify the desired element within the desired time frame hence you faced TimeoutException.
However, from TimeoutException it will be tough to dig out the actual result of the failure.
Solution
As a solution to know about the exact cause of the failure, you need to remove the WebDriverWait and replace the line of code with either:
find_element_by_class_name(name)
find_element_by_css_selector(css_selector)
find_element_by_id(id)
find_element_by_link_text(link_text)
find_element_by_name(name)
find_element_by_partial_link_text(partial_link_text)
find_element_by_tag_name(tag_name)
find_element_by_xpath(xpath)
If required you can slow down the search inducing waits through time.sleep(secs)
while debugging.
References
You can find a couple of relevant discussions in: