zoukankan      html  css  js  c++  java
  • selenium---常用元素等待的三种方法

      在写appium的时候介绍了等待时间,其实selenium这里也是一样的,分别是强制等待,隐式等待,显示等待。详情见:appium---等待时间

    强制等待

    看到名称就应该知道,强制等待,就是设置多少秒,就必须等待多少秒,才能继续往下面操作

    time.sleep()

    def sleep(seconds): # real signature unknown; restored from __doc__
        """
        sleep(seconds)
        
       延迟指定的秒数
        """
        pass

    使用方法

    # 直接在需要等待的地方添加
    import time
    time.sleep(10)

    隐式等待

    隐式等待:  implicitly_wait?()  默认参数的单位为妙,设置一个等待时间,它并不影响脚本的执行速度。当脚本执行到某个元素定位是,如果元素可以定位,则继续执行,如果元素定位不到,则它将以轮询的方式不断地判断元素是否被定位到。假设在第六秒定位到了元素则继续执行,若直到超出设置的时长10秒还没有定位到元素,则抛出异常。

    源码:

    def implicitly_wait(self, time_to_wait):
            """
            Sets a sticky timeout to implicitly wait for an element to be found,
               or a command to complete. This method only needs to be called one
               time per session. To set the timeout for calls to
               execute_async_script, see set_script_timeout.
    
            :Args:
             - time_to_wait: Amount of time to wait (in seconds)
    
            :Usage:
                driver.implicitly_wait(30)
            """
            if self.w3c:
                self.execute(Command.SET_TIMEOUTS, {
                    'implicit': int(float(time_to_wait) * 1000)})
            else:
                self.execute(Command.IMPLICIT_WAIT, {
                    'ms': float(time_to_wait) * 1000})

    使用方法:

    # 在需要等待的地方直接添加
    driver.implicitly_wait(10)

    显示等待

    显式等待是WebDriver等待某个条件成立则继续执行,在等待的时间内,可以多少秒进行查看,看等待条件是否出现。否则在达到最大时长时抛出超时异常(TimeoutException)

    源码:

    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
            """
        driver: 返回一个webdriver实例化
        timeout:设置一个超时时长(S)
        poll_frequency:循环读取元素的时间,默认是0.5(s)
      
                使用方法 :
                from selenium.webdriver.support.ui import WebDriverWait 
    
                element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) 
    
                is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)). 
    
                            until_not(lambda x: x.find_element_by_id("someId").is_displayed())
            """
            self._driver = driver
            self._timeout = timeout
            self._poll = poll_frequency
            # avoid the divide by zero
            if self._poll == 0:
                self._poll = POLL_FREQUENCY
            exceptions = list(IGNORED_EXCEPTIONS)
            if ignored_exceptions is not None:
                try:
                    exceptions.extend(iter(ignored_exceptions))
                except TypeError:  # ignored_exceptions is not iterable
                    exceptions.append(ignored_exceptions)
            self._ignored_exceptions = tuple(exceptions)

    从源码中分许出来三个参数的作用

    driver:返回一个webdriver实例化

    timeout:设置一个超时时长

    poll_frequentcy:循环读取元素时间

    ignored_exceptions:报错信息

    WebDriverWait一般与until()或until_not()方法配合使用。

    until表示:提供一个参数,返回值为True

    until_not表示:提供一个参数,返回值为Flase

    使用方法:

    from selenium.webdriver.support.ui import WebDriverWait
    
    element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))

    一般显示等待可以和EC(expected_conditions)元素等待进行一起使用。

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    driver = webdriver.Chrome()
    driver.implicitly_wait(10) # 隐性等待和显性等待可以同时用,但要注意:等待的最长时间取两者之中的大者
    driver.get('https://www.baidu.com')
    locator = (By.ID, 'kw')
    # 判断元素是否出现 WebDriverWait(driver,
    20,0.5).until(EC.presence_of_element_located(locator))

    如果安静写的有不懂的或者写错的地方,都可以下方留言,安静看到后第一时间进行回复,持续更新~~

  • 相关阅读:
    这一次,Google 终于对 Web 自动化下手了!
    移动端自动化 AutoJS 快速入门指南
    App 端自动化的最佳方案,完全解放双手!
    快过年了,如何使用 AutoJS 自动化快速抢微信红包!
    字节面试问我如何高效设计一个LRU,当场懵
    当前大厂笔试最高频的十道算法题
    coding game, 边打游戏边学编程,是一种怎么样的体验?
    一文详解面试常考的TopK问题
    动态规划,这几个问题最常见!
    备战蓝桥杯,你可以这么准备
  • 原文地址:https://www.cnblogs.com/qican/p/13392263.html
Copyright © 2011-2022 走看看