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

selenium,结合,tenacity,retry,实现,验证码,失败,重试 · 浏览次数 : 369

小编点评

**VIP学员应该掌握的知识:** 1. 识别和处理验证码。 2. 尝试自动登录功能。 3. 捕获异常并进行重试。 4. 设置重试条件。 5. 避免重复重试。

正文

说在前面

  • 验证码登录的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 <module>
    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[<Future at 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中就用到了这个

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

与selenium结合tenacity的retry实现验证码失败重试相似的内容:

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

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

Selenium+2Captcha 自动化+验证码识别实战

> 本文深入探讨了使用Selenium库进行网页自动化操作,并结合2Captcha服务实现ReCAPTCHA验证码的破解。内容涵盖Selenium的基础知识、验证码的分类、2Captcha服务的使用,以及通过实例进行的详细讲解,最后对实践进行总结和优化思考,为读者提供了一条完整的验证码破解实践路线图

selenium库浅析

selenium库浅析 基于4.3 pip install selenium安装好后,在sitepackages下 2个主要的目录,common和webdriver 1- common 该目录一共就一个模块exceptions.py ① exceptions.py 其中定义了32个异常,竟然有个同学

Selenium中对于颜色的处理及拓展

Selenium中对于颜色的处理及拓展 获取百度一下按钮的背景色 from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get('https://www.baidu.com')

Selenium环境搭建

Selenium环境搭建 基于windows 10 素材 素材下载地址说明 python https://registry.npmmirror.com/binary.html?path=python/ 官网 https://mirrors.huaweicloud.com/python/ 国内地址 h

Selenium中免登录的实现方法一option

Selenium中免登录的实现方法一option 在selenium中有很多种可以实现网站的免登录,option就是其中的一种做法。 学员在VIP自动化课程中多有涉及。 1. 准备工作 打开一个网站(此处以https://www.processon.com/为例) 手工登录 关闭 重开该网站发现已经

Selenium中的option用法实例

Selenium中的option用法实例 在上一篇文章Selenium中免登录的实现方法一option中我们用到了option,而option的用法是很多的,本文举几个例子 关于无头浏览器,也属于option的一种,但我们单独开个篇幅讲一下 看完你会发现多数是参数形式的,那chrome作为这么一个大

Selenium CHANGELOG[最新版本4.12.0 23-8-29]

# Selenium CHANGELOG[持续更新] > 源文件 https://github.com/SeleniumHQ/selenium/blob/trunk/py/CHANGES > > 搬运工 > > 对重点版本做时间标注,具体时间点可以参考https://github.com/Selen

selenium-wire兼容selenium和requests

# 背景 在工作中UI自动化中可能会需要用到API来做一些数据准备或清理的事情,那UI操作是略低效的,但API操作相对高效。 而实战课就有这样一个案例,不过那个案例是UI操作和API分开的。 极少会遇到这样的场景,我selenium操作网页,比如登录,应该底层也是有响应数据的,比如token之类的,

Selenium 自动化浏览器,解决懒加载的网页获取问题

Selenium 自动化浏览器,解决懒加载的网页获取问题。可以用于爬虫这些 在使用 Selenium WebDriver 进行自动化测试时,可以通过设置日志级别来控制输出的日志信息。在 C# 中,可以通过以下方式来禁用 Selenium WebDriver 输出的日志信息: 导入 OpenQA.Se