如何获取python-selenium中元素的'state'?
How to get the 'state' of an element in python-selenium?
在python-selenium中有两种方法可以找到一个元素。
首先,您可以使用实际方法查找元素,例如
element = find_element_by_xpath(myxpath)
其次,您可以使用 WebDriverWait
确保 webdriver 等待一些超时,直到在某个给定状态 找到元素 :
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, myxpath)))
对于后一种方法,您可以定义多个 'expected conditions'(参见 here):
element_located_to_be_selected, element_to_be_clickable, element_to_be_selected etc.
我的问题:仅使用第一种查找元素的方法,如何检查该元素处于哪个状态(以防我找到该元素)。如何检查是 'clickable' 还是 'selectable' 等?是否有 element
对象的属性可用于确定元素的 state?
把这个:
driver.findElement(your-element).click()
进入try-catch块,然后处理异常
没有任何 is_clickable()
函数或属性。看着 source code
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
您可以看到 element_to_be_clickable
使用 visibility_of_element_located
,它使用 element.is_displayed()
等待可见性,并使用 element.is_enabled()
检查元素是否启用。您可以检查这两者的组合。
对于 element_located_to_be_selected
你确实有一个函数,element.is_selected()
。
关于 "click()",它没有出现在 Python WebElement API, but in Java's API 它统计 "There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0" 中,检查这些参数就足够了。
像这样:
button = driver.find_element_by_class_name('?')
href_data = button.get_attribute('href')
if href_data is None:
is_clickable = False
No,没有直接的方法通过Selenium[=69]检索WebElement的确切state =].
find_element_by_xpath(xpath)
find_element_by_xpath() would simply find the first matching WebElement using the xpath. If no matching element is found it will raise NoSuchElementException
但是 Selenium 提供了多种方法来验证 WebElement 的状态,如下所示:
is_displayed()
: Returns 一个布尔值 (true / false) 元素是否对用户可见。
is_enabled()
: Returns 一个布尔值 (true / false) 元素是否启用。
is_selected()
: Returns 一个布尔值(true / false) 元素是否被选中。
WebDriverWait() 结合 expected_conditions class
罐装 expected_conditions 通常可用于在元素达到 确定状态 时访问元素,如下所示:
presence_of_element_located(locator)
提及:
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
visibility_of_element_located(locator)
提及:
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
element_to_be_clickable(locator)
提到:
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
An Expectation for checking an element is visible and enabled such that you can click it.
在python-selenium中有两种方法可以找到一个元素。
首先,您可以使用实际方法查找元素,例如
element = find_element_by_xpath(myxpath)
其次,您可以使用 WebDriverWait
确保 webdriver 等待一些超时,直到在某个给定状态 找到元素 :
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, myxpath)))
对于后一种方法,您可以定义多个 'expected conditions'(参见 here):
element_located_to_be_selected, element_to_be_clickable, element_to_be_selected etc.
我的问题:仅使用第一种查找元素的方法,如何检查该元素处于哪个状态(以防我找到该元素)。如何检查是 'clickable' 还是 'selectable' 等?是否有 element
对象的属性可用于确定元素的 state?
把这个:
driver.findElement(your-element).click()
进入try-catch块,然后处理异常
没有任何 is_clickable()
函数或属性。看着 source code
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
您可以看到 element_to_be_clickable
使用 visibility_of_element_located
,它使用 element.is_displayed()
等待可见性,并使用 element.is_enabled()
检查元素是否启用。您可以检查这两者的组合。
对于 element_located_to_be_selected
你确实有一个函数,element.is_selected()
。
关于 "click()",它没有出现在 Python WebElement API, but in Java's API 它统计 "There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0" 中,检查这些参数就足够了。
像这样:
button = driver.find_element_by_class_name('?')
href_data = button.get_attribute('href')
if href_data is None:
is_clickable = False
No,没有直接的方法通过Selenium[=69]检索WebElement的确切state =].
find_element_by_xpath(xpath)
find_element_by_xpath() would simply find the first matching WebElement using the xpath. If no matching element is found it will raise NoSuchElementException
但是 Selenium 提供了多种方法来验证 WebElement 的状态,如下所示:
is_displayed()
: Returns 一个布尔值 (true / false) 元素是否对用户可见。is_enabled()
: Returns 一个布尔值 (true / false) 元素是否启用。is_selected()
: Returns 一个布尔值(true / false) 元素是否被选中。
WebDriverWait() 结合 expected_conditions class
罐装 expected_conditions 通常可用于在元素达到 确定状态 时访问元素,如下所示:
presence_of_element_located(locator)
提及:class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator) An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
visibility_of_element_located(locator)
提及:class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator) An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
element_to_be_clickable(locator)
提到:class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator) An Expectation for checking an element is visible and enabled such that you can click it.