【问题标题】:How to get the 'state' of an element in python-selenium?如何在 python-selenium 中获取元素的“状态”?
【发布时间】:2018-09-27 08:17:42
【问题描述】:

在 python-selenium 中有两种查找元素的方法。

首先,你可以使用实际的方法来查找一个元素,例如

element = find_element_by_xpath(myxpath)

其次,您可以使用 WebDriverWait 确保 webdriver 等待一段时间,直到找到元素处于某个给定状态

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, myxpath)))

对于后一种方法,您可以定义几个“预期条件”(参见here):

element_located_to_be_selected, element_to_be_clickable, element_to_be_selected etc.

我的问题:仅使用查找元素的第一种方法,我如何检查该元素处于哪个状态(以防我找到该元素)。如何检查是否“可点击”或“可选择”等?我可以使用element 对象的属性来确定元素的状态吗?

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

    没有任何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 中,但在 Java's API 中它 stats “有一些先决条件才能点击一个元素。该元素必须是可见的并且它的高度和宽度必须大于 0",检查这些参数就足够了。

    【讨论】:

      【解决方案2】:

      ,没有直接的方法可以通过 Selenium 检索 WebElement 的确切状态


      find_element_by_xpath(xpath)

      find_element_by_xpath() 将简单地使用 xpath 找到第一个匹配的 WebElement。如果没有找到匹配的元素,它将引发NoSuchElementException

      但是 Selenium 提供了多种方法来验证 WebElement 的状态,如下所示:

      • is_displayed():返回一个布尔值(真/假),无论该元素对用户是否可见。

      • is_enabled():无论元素是否启用,都返回一个布尔值(真/假)。

      • is_selected():无论元素是否被选中,都返回一个布尔值(真/假)。


      WebDriverWait() 与 expected_conditions 类结合

      罐头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.
        

      【讨论】:

        【解决方案3】:

        把这个: driver.findElement(your-element).click() 进入try-catch块,然后处理异常

        【讨论】:

          【解决方案4】:

          类似这样的:

          button = driver.find_element_by_class_name('?')
          href_data = button.get_attribute('href')
          if href_data is None:
          is_clickable = False
          

          【讨论】:

          • 并非所有可点击元素都重定向,元素可以点击并执行没有href属性的操作。
          猜你喜欢
          • 2018-06-16
          • 1970-01-01
          • 2017-08-22
          • 2019-07-21
          • 1970-01-01
          • 2022-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多