I have a page object which interacts with few elements on the DOM. If I create a WebDriverWait
object on my page object initialization as an instance member, can I use it for all of the waits?
Or if I wanna wait for two separate element, it's better to have two WebDriverWait
s?
I'm experiencing some weird TimeoutException
s and I wonder it might be that. Like:
class MyPage(object):
def __init__(self, driver):
self.driver = driver
self.wait = WebDriverWait(driver, 10)
def get_search_box(self):
return self.wait.until(EC.presence_of_element_located('srch'))
def get_search_btn(self):
return self.wait.until(EC.presence_of_element_located('btn'))
versus:
def get_search_btn(self):
wait = WebDriverWait(self.driver, 10)
return wait.until(EC.presence_of_element_located('btn'))
I guess what you need is a function which you can reuse as required. Take a look at the following function for example:
def wait_for_element_to_be_visible(self, *locator):
"""Wait for an element to become visible"""
self.selenium.implicitly_wait(0)
try:
WebDriverWait(self.selenium, self.timeout).until(
lambda s: self._selenium_root.find_element(*locator).is_displayed())
except TimeoutException:
Assert.fail(TimeoutException)
finally:
self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With