当前位置: 首页 > 知识库问答 >
问题:

硒,存在于许多元素中的一种定位?

夏奕
2023-03-14

在这种情况下使用any()的正确方式是什么?或者更好的是,需要做什么才能使这种方法发挥作用?

假设Selenium.get('url')在调用此方法之前已经执行。

def wait_with_xpath_expectation(self, search_elements, timeout=6, max_attempts=3):
    """
    Selenium wait for an element(s) designated by Xpath to become available in the DOM. Useful for javascript AJAXy
    loading pages where content may be be rendered dynamically on the client side after page load appears complete.
    search_elements may be one Xpath as a string or many as a list. This allows for any of multiple elements on a
    page or pages to be determined to have loaded based on expectations.
    :param search_elements: string or list (strings converted to lists), Xpath(s)
    :param timeout: int, seconds
    :param max_attempts: int, time to wait before giving up on page loading
    :return: Boolean, True if page has loaded, False if all attempts have failed
    """

    # Page is not loaded yet
    loaded = False

    # Initialize attempt count
    attempt = 0

    # If only one element has been passed, ensure it is encapsulated by a list
    if type(search_elements) is str:
        search_elements = [search_elements]

    # Begin the polling loop
    while attempt < max_attempts:

        try:

            while loaded is False:
                # Create a list of expected elements using list comprehension
                expectations = [EC.presence_of_element_located((By.XPATH, element)) for element in search_elements]

                # Define wait
                wait = WebDriverWait(self.browser, timeout)

                # Execute
                wait.until(any(expectations))

                # Acknowledge load
                loaded = True

                print("Success: Page loaded based on expectations")

                # Exit returning True for load
                return loaded

        except TimeoutException as e:

            # Increment attempts
            attempt += 1

            # Check again if max attempts has not been exceeded
            while attempt < max_attempts:

                # Increase timeout by 20%
                timeout *= .2

                # Try again 
                continue

            # Print an error if max attempts is exceeded
            print("Error: Max load with expectations attempts exceeded,", e)

            # Exit returning False for load
            return loaded

共有1个答案

杨景山
2023-03-14

您可以有一个预期条件类来等待预期条件的组合。这里有一个例子。

class wait_for_all(object):
    def __init__(self, methods):
        self.methods = methods

    def __call__(self, driver):
        try:
            for method in self.methods:
                if not method(driver):
                    return False
            return True
        except StaleElementReferenceException:
            return False

然后通过构建预期条件的数组并在相同的等待中检查所有条件来使用此方法。(为清晰起见,示例行被拆分。)

methods = []
methods.append(EC.visibility_of_element_located(BY.ID, "idElem1"))
methods.append(EC.visibility_of_element_located(BY.ID, "idElem2"))
method = wait_for_all(methods)
WebDriverWait(driver, 5).until(method)

这将执行一个五秒的等待,同时检查两个不同元素的可见性。

 类似资料:
  • 但是它抛出了-看起来像期望元素在那里,所以这是有缺陷的。这对硒来说一定是面包和黄油,不想推倒重来……有没有人可以提出一个替代方案,理想情况下不滚动我自己的?

  • 在实时自动化中,在对每个元素执行操作之前,我们是否检查它们的存在(在测试中)? 只要有一个语句,就有可能出现NoTouchElementException。我的问题是我们是否每次都检查元素的存在? 是否每个语句都需要被块包围?

  • 我正在测试用户单击删除按钮和表条目消失的UI。因此,我希望能够检查表条目是否不再存在。 我已经尝试使用< code > expected conditions . not()来反转< code > expected conditions . presenceofelementlocated(),希望它意味着“期望不存在指定的元素”。我的代码是这样的: 但是,我发现即使这样做,我也会得到一个由引起的

  • 问题内容: 我试图让Selenium等待页面加载后动态添加到DOM的元素。试过这个: 如果有帮助,这里是: 但是它抛出一个-看起来像期望元素存在,所以这是有缺陷的。这一定是Selenium的面包和黄油,不想重新发明轮子……任何人都可以提出其他选择,理想情况下不用自己动手做? 问题答案: 需要等待时,您需要异常调用以忽略。 有关更多信息,请参见FluentWait文档。但是请注意,此条件已经在Exp

  • 我正在测试一个仍在开发中的网站。 元素的id、类、文本或在DOM中的位置通常会改变。然后我使用的定位器将无法再找到元素。 但是这些功能仍然正常运行。我不希望几个测试在没有实际回归的情况下失败。 因此,我没有为每个元素使用一个定位器,而是使用了一组定位器。 我查找元素的方法如下所示: 它尝试查找集合中具有第一个定位器的元素,只有在失败时,才尝试下一个定位器。 集合是一个(顺序由插入顺序定义),这意味

  • 我正在使用selenium创建kahoot机器人泛滥器。(kahoot.it)我正在尝试使用selenium来定位输入框以及确认按钮。每当我尝试将它们定义为变量时,我都会得到这个。"命令引发了一个异常:TimeoutException: Message:",我认为这意味着我设置的5秒已过期,这意味着该元素从未被定位。 我试图定位一个“Iframe ”,但并不成功(可能做错了),但我已经搜索了几个小