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?
我有一个页面对象,它与DOM上的少数元素交互。如果我在页面对象初始化上创建WebDriverWait对象作为实例成员,我可以将它用于所有等待吗?
Or if I wanna wait for two separate element, it's better to have two WebDriverWait
s?
或者,如果我想等待两个单独的元素,最好有两个WebDriverWaits?
I'm experiencing some weird TimeoutException
s and I wonder it might be that. Like:
我遇到了一些奇怪的TimeoutExceptions,我想它可能就是这样。喜欢:
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'))
1 个解决方案
#1
2
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)
#1
2
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)