• selenium结合tenacity的retry实现验证码失败重试


    说在前面

    • 验证码登录的demo后续可以单独讲解,VIP学员对这部分应该都是掌握的,此处不再赘述
    • 本文假设了一个场景
      • 你通过OCR识别的验证码是有一定的错误几率的
      • 本文是通过识别后的验证码去加一个随机字符,如果取到的是''空字符则可能成功,否则必然不会成功
    • 所涉及的python库
      • selenium
      • ddddocr
      • tenacity

    上代码

    • 细节详见注释
    from selenium import webdriver
    from time import sleep
    from tenacity import TryAgain, retry, wait_random
    
    
    def get_element(locator):
        '''
        这个函数用来判断是否存在某个元素
        '''
        try:
            from selenium.webdriver.support.wait import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC
            return WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located(locator))
        except:
            return False
    
    
    driver = webdriver.Chrome()
    driver.implicitly_wait(5)
    driver.get('http://114.116.2.138:8090/forum.php')  # 这个地址你可以访问,是我搭建在云端的一个容器,开源论坛
    driver.find_element('css selector', '#ls_username').send_keys('admin')
    driver.find_element('css selector', '#ls_password').send_keys('123456')
    driver.find_element('css selector', 'button.pn.vm').click()
    
    
    @retry(wait=wait_random(min=3, max=5)) # 等待一个时间区间,3-5s,注意这个时间的下限建议>hint存在的最大时间
    def code_login():
        '''
        这个函数是用来反复验证码登录的
        '''
        # 验证码元素
        ele_code = driver.find_element('css selector', '[id^=vseccode_cS]>img')
        import ddddocr
        ocr = ddddocr.DdddOcr()
        code_text = ocr.classification(ele_code.screenshot_as_png)
        # 当失败的时候尤其要注意: 清空已输入的内容
        driver.find_element('css selector', 'input[id^=seccodeverify_cS]').clear()
        test_data = ['1','a','','2','b']
        from random import choice
        choice_data = choice(test_data)
        # 输入识别后的数据+随机字符
        driver.find_element('css selector', 'input[id^=seccodeverify_cS]').send_keys(code_text + choice_data)
        # 点击登录
        driver.find_element('css selector', "[name='loginsubmit']>strong").click()
        # 注意! 你可以去操作网页,点击登录如果失败会弹出提示 "抱歉,验证码填写错误"
        hint_locator = 'css selector', '.pc_inner>i'
        if get_element(hint_locator): # 如果出现这个元素,就...
            # 点击下验证码,刷新一下
            driver.find_element('css selector', '[id^=vseccode_cS]>img').click()
            # 抛出异常,重跑
            raise TryAgain
    code_login()
    

    聊聊tenacity

    https://tenacity.readthedocs.io/en/latest/api.html

    https://github.com/jd/tenacity

    • 这个库将重试这件事基本做透了

    1、 无条件重试

    • 你疯了
    from tenacity import retry
    
    @retry
    def retry_without_anything():
        print('retry...')
        raise Exception  # 不加这个可不会重试
    
    retry_without_anything()
    

    2、重试指定次数后停止

    from tenacity import retry, stop_after_attempt
    @retry(stop=stop_after_attempt(3))
    def retry_times():
        print(f'retry... times')
        raise Exception
    
    
    retry_times()
    
    
    • 实际运行的效果是这样
    retry... times
    retry... times
    retry... times
    Traceback (most recent call last):
      File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 407, in __call__
        result = fn(*args, **kwargs)
      File "demo_retry.py", line 20, in retry_times
        raise Exception
    Exception
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "demo_retry.py", line 23, in 
        retry_times()
      File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 324, in wrapped_f
        return self(f, *args, **kw)
      File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 404, in __call__
        do = self.iter(retry_state=retry_state)
      File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 361, in iter
        raise retry_exc from fut.exception()
    tenacity.RetryError: RetryError[0x16628b45460 state=finished raised Exception>]
    
    • 别担心,你可能在最后一次的时候就不抛出异常了。

    3、过一定时间后停止重试

    from tenacity import retry, stop_after_delay
    import  arrow
    from time import sleep
    @retry(stop=stop_after_delay(10))
    def retry_times():
        print(arrow.now().format('YYYY-MM-DD HH:mm:ss'))
        sleep(1)
        raise Exception
    retry_times()
    
    • 输出像这样
    2023-02-21 17:32:01
    2023-02-21 17:32:02
    2023-02-21 17:32:03
    2023-02-21 17:32:04
    2023-02-21 17:32:05
    2023-02-21 17:32:06
    2023-02-21 17:32:07
    2023-02-21 17:32:08
    2023-02-21 17:32:10
    2023-02-21 17:32:11
    # 最后抛出异常
    

    4、组合条件

    @retry(stop=(stop_after_delay(10) | stop_after_attempt(5)))
    def retry_multi_conditions():
        print("10秒后或者5次重试后停止重试")
        raise Exception
    

    5、重试间隔

    • 之前的重试是无缝衔接的

    • 你可以让重试之间有延迟

      @retry(wait=wait_fixed(2))
      def retry_wait_time1():
          print(arrow.now().format('YYYY-MM-DD HH:mm:ss'))
          print("每次重试前等待2秒")
          raise Exception
      
      retry_wait_time1()
      
    • 当然上面的仍然是个无限重试

    • 你可以组合前面的停止

      @retry(wait=wait_fixed(2),stop=stop_after_attempt(3))
      

    • 重试等待间隔可以设定一个区间(最大最小值)

      from tenacity import retry, stop_after_attempt, wait_random
      import  arrow
      
      
      @retry(wait=wait_random(min=1,max=4),stop=stop_after_attempt(3))
      def retry_wait_time1():
          print(arrow.now().format('YYYY-MM-DD HH:mm:ss'))
          print("每次重试前等待1~4秒区间")
          raise Exception
      
      retry_wait_time1()
      

    6、是否重试!!!

    • 这是最重要的了

    • 重试的条件一:引发特定或一般异常的重试

      from tenacity import retry, retry_if_exception_type, stop_after_attempt
      
      @retry(retry=retry_if_exception_type(ZeroDivisionError),
                    stop= stop_after_attempt(5))
      def retry_if_exception():
          print('retry...')
          print(10/0)   # 现在触发的就是ZeroDivisionError,如果把此处改为 print(a),则遇到的是NameError,那就不会重试
          raise Exception
      
      retry_if_exception()
      

    • 引发 TryAgain 异常随时显式重试

    • 比如这样

      @retry
      def try_if_condition():
          result = 23
          if result == 23:
             raise TryAgain
      
    • 上面的demo中就用到了这个

    • 官网还有很多的例子,相对高级一些,如有兴趣可以自行前往或者搜索之

  • 相关阅读:
    Unity清除项目中的不用的文件,Unity源文件体积缩小,Unity减小EXE体积
    设计模式-组合模式
    Linux入门教程:P10->用户权限类
    安卓手机格式化后怎么恢复
    半马尔科夫决策过程
    MySQL数据类型总结与使用
    hive insert values 方式
    Apache Calcite - 使用内置函数
    二叉堆(基础)
    Python的格式化输出
  • 原文地址:https://www.cnblogs.com/wuxianfeng023/p/17141882.html