[🚀 Feature]: Methods awaiting conditions
Feature and motivation
It would be nice if the classes WebDriver, WebElement, ShadowRoot, and EventFiringWebDriver had methods that waited for expected_conditions with respect to themselves. In Python there could be an abstract base class
class WebTree(metaclass=ABCMeta):
from which these classes are inherited. In Python it could have the abstract methods
@abstractmethod
def find_element(self, by=By.ID, value=None):
pass
@abstractmethod
def find_elements(self, by=By.ID, value=None):
pass
and the implemented methods:
def await_condition(self, condition, *args, **kwargs):
return WebDriverWait(self, *args, **kwargs).until(condition)
def await_element(self, by=By.ID, value=None, *args, **kwargs):
return self.await_condition(presence_of_element_located((by, value)), *args, **kwargs)
def await_button(self, by=By.ID, value=None, *args, **kwargs):
return self.await_condition(element_to_be_clickable((by, value)), *args, **kwargs)
Usage example
Assuming a default timeout is added to WebDriverWait, you could use the following functions:
element = webdriver.await_element(By.ID, "element")
subelement = element.await_element(By.ID, "subelement ", timeout=10)
shadowroot = subelement.await_element(By.ID, "shadowroot", poll_frequency=0.1).shadow_root
button = shadowroot.await_button(By.ID, "button", ignored_exceptions=[NoSuchElementException])
@BobbyCephy, thank you for creating this issue. We will troubleshoot it as soon as we can.
Info for maintainers
Triage this issue by using labels.
If information is missing, add a helpful comment and then I-issue-template label.
If the issue is a question, add the I-question label.
If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.
If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C),
add the applicable G-* label, and it will provide the correct link and auto-close the
issue.
After troubleshooting the issue, please add the R-awaiting answer label.
Thank you!
Thanks for the suggestion. I recommend creating your own wrapper class(es) for elements that stores the locator and caches the element. Unfortunately the feature is out of scope for this project.
Thank you for you feedback! Anyway I think waiting for an element with
element = webdriver.await_element(By.ID, "element")
and a default timeout would more intuitive, especially for beginners, in the same context than:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
element = WebDriverWait(webdriver, 10).until(presence_of_element_located((By.ID, "element"))
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.