Skip to content

希望能升级对selenium4的支持 #10

@zjlx

Description

@zjlx

在本地selenium4的情况下运行airtest_selenium的driver.find_element_by_id
会提示UserWarning: find_element_by_* commands are deprecated. Please use find_element() instead
这个warning不理会也没问题,还是觉得老的写法方便。
下面我把proxy.py文件里的loop_find_element增加了个by参数

def loop_find_element(self, func,by, text, timeout=10, interval=0.5):
        """
        Loop to find the target web element by func.

        Args:
            func: function to find element
            text: param of function
            timeout: time to find the element
            interval: interval between operation
        Returns:
            element that been found
        """
        start_time = time.time()
        while True:
            try:
                element = func(by,text)
            except NoSuchElementException:
                print("Element not found!")
                # 超时则raise,未超时则进行下次循环:
                if (time.time() - start_time) > timeout:
                    # try_log_screen(screen)
                    raise NoSuchElementException(
                        'Element %s not found in screen' % text)
                else:
                    time.sleep(interval)
            else:
                return element

把find_element_by_id改成了如下

    def find_element_by_id(self, id):
        """
        Find the web element by id.

        Args:
            id: find the element by attribute id.
        Returns:
            Web element of current page.
        """
        web_element = self.loop_find_element(super(WebChrome, self).find_element,By.ID, id)
        log_res = self._gen_screen_log(web_element)
        return Element(web_element, log_res)

这样既能享受老版本的find_element_by_id的方便,还不会因为selenium版本太高而总弹出warning,不会pr,只能贴到这里了,希望能更新下,非常感谢。

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions