selnium三种等待方式
直接等待
time.sleep(3)
隐式等待:全局等待
dirver.implicitly_wait(5) 初始化的时候设置就行
显示等待
需要导入WebDriverWait(self.driver)
WebDriverWait(self.driver,10).until(expected_conditions.element_to_be_clickable((By.ID,"id")))
可以到until方法里边看到 其实里边是一个循环
def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is not False."""
screen = None
stacktrace = None
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
expected_conditions是一个库,期望条件,库里有很多条件类比如:
class visibility_of_all_elements_located(object):
""" An expectation for checking that all elements are present on the DOM of a
page and visible. Visibility means that the elements are not only displayed
but also has a height and width that is greater than 0.
locator - used to find the elements
returns the list of WebElements once they are located and visible
"""
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
try:
elements = _find_elements(driver, self.locator)
for element in elements:
if _element_if_visible(element, visibility=False):
return False
return elements
except StaleElementReferenceException:
return False
class element_to_be_clickable(object):
""" An Expectation for checking an element is visible and enabled such that
you can click it."""
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
element = visibility_of_element_located(self.locator)(driver)
if element and element.is_enabled():
return element
else:
return False