本文整理汇总了Python中selenium.webdriver.support.ui.WebDriverWait.ensure_click方法的典型用法代码示例。如果您正苦于以下问题:Python WebDriverWait.ensure_click方法的具体用法?Python WebDriverWait.ensure_click怎么用?Python WebDriverWait.ensure_click使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.support.ui.WebDriverWait
的用法示例。
在下文中一共展示了WebDriverWait.ensure_click方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ensure_element
# 需要导入模块: from selenium.webdriver.support.ui import WebDriverWait [as 别名]
# 或者: from selenium.webdriver.support.ui.WebDriverWait import ensure_click [as 别名]
def ensure_element(self, locator, selector, state="present", timeout=None):
"""This method allows us to wait till an element appears or disappears in the browser
The webdriver runs in parallel with our scripts, so we must wait for it everytime it
runs javascript. Selenium automatically waits till a page loads when GETing it,
but it doesn't do this when it runs javascript and makes AJAX requests.
So we must explicitly wait in that case.
The 'locator' argument defines what strategy we use to search for the element.
The 'state' argument allows us to chose between waiting for the element to be visible,
clickable, present, or invisible. Presence is more inclusive, but sometimes we want to
know if the element is visible. Careful, its not always intuitive what Selenium considers
to be a visible element. We can also wait for it to be clickable, although this method
is a bit buggy in selenium, an element can be 'clickable' according to selenium and
still fail when we try to click it.
More info at: http://selenium-python.readthedocs.io/waits.html
"""
locators = {'id': By.ID,
'name': By.NAME,
'xpath': By.XPATH,
'link_text': By.LINK_TEXT,
'partial_link_text': By.PARTIAL_LINK_TEXT,
'tag_name': By.TAG_NAME,
'class_name': By.CLASS_NAME,
'css_selector': By.CSS_SELECTOR}
locator = locators[locator]
if not timeout: timeout = self.default_timeout
if state == 'visible':
element = WebDriverWait(self, timeout).until(
EC.visibility_of_element_located((locator, selector))
)
elif state == 'clickable':
element = WebDriverWait(self, timeout).until(
EC.element_to_be_clickable((locator, selector))
)
elif state == 'present':
element = WebDriverWait(self, timeout).until(
EC.presence_of_element_located((locator, selector))
)
elif state == 'invisible':
WebDriverWait(self, timeout).until(
EC.invisibility_of_element_located((locator, selector))
)
element = None
else:
raise ValueError(
"The 'state' argument must be 'visible', 'clickable', 'present' "
"or 'invisible', not '{}'".format(state)
)
# We add this method to our element to provide a more robust click. Chromedriver
# sometimes needs some time before it can click an item, specially if it needs to
# scroll into it first. This method ensures clicks don't fail because of this.
if element:
element.ensure_click = partial(_ensure_click, element)
return element