说在前面

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

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

最新文章

  1. c# 利用结构体获取json数据
  2. MySQL 主键范围查找问题
  3. 如何在eclipse中使用XYLayout布局?在此介绍如何把XYLayout导入到eclipse .
  4. dom4j api 详解--XPath 节点详解
  5. C#_datatable 写入大量数据_BulkCopy
  6. Contains Duplicate II 解答
  7. 使用vue-cli脚手架搭建简单项目框架
  8. APICloud常用模块
  9. LVS中Windows作为真实主机(RealServer)时的设置方法
  10. iOS UIFileSharingEnabled
  11. IdentityServer4结合AspNetCore.Identity实现登录认证踩坑填坑记录
  12. Unity对象池管理
  13. Spark2 jar包运行完成,退出spark,释放资源
  14. 设置全局context变量 (显示用户登录名)
  15. 关于修改计算机名称导致无法启动Oracle监听?
  16. spring mvc入门配置
  17. Node.js:Buffer(缓冲区)介绍及常用方法
  18. 3dsmax2017卸载/安装失败/如何彻底卸载清除干净3dsmax2017注册表和文件的方法
  19. MySQL优化器不使用索引的情况
  20. agruments应用——求出函数参数的总合&amp;&amp;css函数——设置/读取对象的属性&amp;&amp;当前输入框高亮显

热门文章

  1. 关于解决windows安装gcc g++环境 mingw失败
  2. 第二十六节:urllib、requests、selenium请求库代理设置
  3. 【Java Web】项目通用返回模块ServerResponse:枚举code状态码、泛型返回值、序列化注解限制数据
  4. war包形式安装jenkins
  5. 12、HSSFWorkbook实现多张sheet导出
  6. MySql索引下推知识分享
  7. 中国蚁剑 - AntSword
  8. 使用Prometheus监控docker compose方式部署的ES
  9. linux基础:1、linux简介、虚拟化软件的安装与配置、Xshell的安装与配置
  10. MySql树形结构(多级菜单)查询设计方案