Wanted to ask if there is a chance to create so called extension method for WebElement class within Selenium/Appium framework. I realize that Python does not have extension methods, but some things can be achieved with monkey patching, however I've been strugling to do so.
Let me show it on example
In my framework I have function for looking for elements:
def find_element_with_wait(self, findby_and_locator, time_to_wait=5, dynamicaly_created=False):
"""Finds element on the screen with 5 seconds timeout as default. Timeout can be specified in function parameters as integer. Returns WebElement if element exists and None whene there is no such element"""
find_by, selector = None, None
if isinstance(findby_and_locator, dict):
if DeviceData()._platformName == 'iOS':
find_by, selector = findby_and_locator.get('iOS')
else:
find_by, selector = findby_and_locator.get('Android')
elif isinstance(findby_and_locator, tuple):
find_by, selector = findby_and_locator
self._wait_for_DOM_presence(find_by, selector, time_to_wait)
try:
element = self.driver.find_element(find_by, selector)
except NoSuchElementException:
print(' Seeked element was not found. Return element = None')
element = None
Now as I already have found the element which is object of WebElement class I would like to execute the same function as above on this element, to find another element(child, descendant) inside.
Is it possible to achive such a thing in Python? I did this in C# but in this case I'm helpless.
That would make it easier for me to write tests for my apps
WebElement.find_element_with_wait = find_element_with_wait. Note that you'll have to replaceself.driverbyself._parent. - Florent B.WebElementneed to be from the importselenium.webdriver.remote.webelement.WebElement. - Florent B.