当前位置:网站首页>Web automation processing "sliding verification code"
Web automation processing "sliding verification code"
2022-07-22 01:05:00 【Xiao Zeng com】
Even if the dark clouds dye the sky , The rising sun is still shining somewhere
Verification Code
There are many kinds of , We often have SMS verification code 、 Voice verification code 、 Character verification code 、 Image verification code 、 Slide verification code
etc. . Today, I mainly share it with my friends in web automated testing
in , How to handle sliding verification code ?
First , There are several ways to deal with sliding verification codes :
- The image processing
- adopt cookie Bypass
- Interface bypass
Today's main sharing is through The image processing
Method to handle sliding verification code .
With Douban official website For example , After entering the official website , Click password to log in , After entering the account number and password , Click login , You can jump out of the sliding verification code , Then the manual operation can be successful . Now we use an automated way to operate this series of steps .
The whole process can be divided into the following steps
- Show captcha
- Get the picture address of slider and skateboard
- Download sliders and skateboard pictures
Artificial intelligence
Match the slider verification code distance (opencv
)- Scaling and calibration slider offset
ActionChains
Sliding unlocking- Add retry mechanism after sliding failure
Environmental preparation :
pip install opencv-python
The complete code is as follows
import cv2
from selenium import webdriver
import requests
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
while True:
#1. Display sliding verification code
driver.get('https://www.douban.com/')
driver.implicitly_wait(10)
sleep(3)
driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="anony-reg-new"]/div/div[1]/iframe'))
driver.find_element_by_xpath('/html/body/div[1]/div[1]/ul[1]/li[2]').click()
driver.find_element(By.XPATH,"//*[@id='username']").send_keys("18888888888")
driver.find_element(By.XPATH,'//*[@id="password"]').send_keys("123456")
driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
sleep(2)
#2. Get the slider and skateboard to the picture address
driver.switch_to.frame(driver.find_element(By.XPATH,'//*[@id="tcaptcha_iframe"]'))
##2.1 slider
slideBlock = driver.find_element(By.XPATH,'//*[@id="slideBlock"]')
slideBlock_url = slideBlock.get_attribute("src")
##2.2 Skate
slideBg = driver.find_element(By.XPATH,'//*[@id="slideBg"]')
slideBg_url = slideBg.get_attribute("src")
#print(slideBg_url)
#print(slideBlock_url)
#3. Download sliders and skateboards
with open('images/big_images.jpg','wb') as f:
f.write(requests.get(slideBg_url).content)
f.close()
with open('images/small_images.jpg','wb') as f:
f.write(requests.get(slideBlock_url).content)
f.close()
#4. After the small picture of intelligent matching slider and skateboard overlaps x Distance of
big_grag = cv2.imread("images/big_images.jpg",0) # Load image in grayscale mode
small_grag = cv2.imread("images/small_images.jpg",0) # Load image in grayscale mode )
res = cv2.matchTemplate(big_grag,small_grag,cv2.TM_CCORR_NORMED) # A match object
cv2.imread
value = cv2.minMaxLoc(res)
print("value Value ",value)
x = value[2][0]
print(x)
#5. Scaling and calibration slider offset , The original picture is 680*390, the truth is that 282*162
x = int(x*282/680)
py = 31 - int(20*282/680)
x = x-py
print(x)
#6. adopt ActionChains Sliding unlocking
hk_ele = driver.find_element(By.XPATH,'//*[@id="slideBlock"]')
action = ActionChains(driver) # Initialize a mouse object
action.click_and_hold(hk_ele).perform() # Hold down the mouse
action.drag_and_drop_by_offset(hk_ele,x,0).perform() # Move the slide to the specified coordinates ,0 Represents the ordinate
sleep(3)
## If the element does not exist , That means success
#7. Add retry mechanism after sliding failure
try:
driver.find_element(By.XPATH,'//*[@id="captcha_close"]/div')
except Exception as e:
break
If you feel good , Give me a compliment , This summer , Let's make progress together .
边栏推荐
- Unity学习笔记-热更新相关
- Read and understand | how data center supports enterprise digital operation
- Hcip day 9
- Strategy Center -- the brain for enterprises to build digital intelligence operation platform
- Redis 官方可视化工具,高颜值,功能真心强大!
- BT.656接口速率
- Lens calibration board
- 三相差分编码器转成脉冲信号或集电极开路转换模块
- Solve the problem that Visio closed pattern cannot be filled
- The only "magic weapon" of enterprise operation and management: data center supported by technical capability matrix
猜你喜欢
DOM -- style operation
温度信号测量K型热电偶信号采集器rs485/232远程IO转换模块
Sword finger offer_ Knowledge transfer ability
Typescript (II)
Hcip day 9
[ar Foundation] ar Foundation Foundation Foundation Foundation
Halcon and opencv finally chose the latter
攻防技术第一篇之-知彼(攻击手段)
Win10 display auto repair fails to boot normally
【网络安全】面试中常见问题--sql注入篇
随机推荐
dom——style的操作
攻防技术第二篇之-知己(防御手段)
Take the interface test in 2 hours - dark horse programmer, master the preparation of interface use cases, interface information analysis, interface information analysis through Oh packet capturing, a
MySQL and Mongo database warehousing in scratch pipeline
What platform can accommodate knowledge base, indicator base and rule base at the same time?
Redis 官方可视化工具,高颜值,功能真心强大!
类模板重载输出运算符报错
PHP如何查询mysql数据库中有无指定表
"Can't buy" digital transformation, each family's "LEGO" is different
How will Import SQL file into MySQL
< a> The tag jumps to the server page and realizes the transfer of parameters
Typescript (II)
dom——预加载和懒加载
解读符合新时代主流的创客教育模式
ROS 多坐标变换
解析惠及中小学校的Steam教育
Script loads data into JSON file
TCP/IP协议栈Lwip的设计与实现:之七
进程知识概括
镜头标定板秩事