Selenium --- sliding box verification code cracking

foreword

At present, there are many kinds of common verification codes, such as digital verification codes, sliding verification codes, and sliding completion image verification codes. The operation of verification codes is a big obstacle in our UI automation. Today I will introduce how to pass Python to realize our sliding verification code

Slide verification code

Let's take a picture first to get acquainted with what the sliding verification code that will be cracked today looks like.

train of thought

According to our normal operation, first press and hold the sliding verification code, and then move to the right. For this operation here, many people will definitely say that we can use the simulated mouse operation in selenium, and today it is done through the mouse operation in selenium. Of course, the premise must be to position the sliding frame first. Here you also need to check how many coordinate points are moved by the mouse, because during manual operation, you can clearly see where to move, but the code level is not clear, how far you want to move. Now that you have a clear idea, let’s do it directly.

practice

Check it with F12. It is found that there are pseudo-elements under the element (the page element does not exist itself, there is no such element in the HTML code, but when the page is displayed, you can see that these elements that do not exist originally play a role), then here through CSS to locate.

Swipe manually to see how many coordinate points we want to move. When we move, we will find that there is an attribute style in HTML that keeps changing. When we move to the end, we find that the value will not change, which is our moving coordinate point. Here you can see the desired offset and the content of the moved element through F12, and then practice the code method

Write the code, here is the code directly, there are comments in the code,

from selenium import webdriver
# 导入selenium中鼠标库
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
driver.get("https://www.jq22.com/yanshi23642")
# 页面最大化
driver.maximize_window()
# 跳转到iframe中
driver.switch_to.frame('iframe')
# 在输入框中输入内容
driver.find_element_by_name('title').send_keys('123456')
# 通过CSS定位滑动点坐标
slider = driver.find_element_by_css_selector('div.slider-btn.layui-icon.layui-icon-next')
time.sleep(5)
action = ActionChains(driver)
# 长按鼠标
action.click_and_hold(slider)
# 偏移量(F12中查看)
action.move_by_offset(268, 0)
# 释放鼠标
action.release()
# 执行以上操作
action.perform()

After executing the code, you will find that it has been implemented.

Whether the verification is successful

The above code and ideas are very clear, so what if we want to assert it? Here you will see that there is actually a prompt "slide verification passed". We can locate it by displaying the waiting method. If the font is correct, it means that the sliding verification code has passed.

positioning text popup

For this kind of passing toast, when the text pops up, you can click to pause through the sources in F12, and then check the element content

Element content:

Just locate the element directly in the code and get the text attribute in it.

from selenium import webdriver
# 导入selenium中鼠标库
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.jq22.com/yanshi23642")
# 页面最大化
driver.maximize_window()
# 跳转到iframe中
driver.switch_to.frame('iframe')
# 在输入框中输入内容
driver.find_element_by_name('title').send_keys('123456')
# 通过CSS定位滑动点坐标
slider = driver.find_element_by_css_selector('div.slider-btn.layui-icon.layui-icon-next')
time.sleep(5)
action = ActionChains(driver)
# 长按鼠标
action.click_and_hold(slider)
# 偏移量(F12中查看)
action.move_by_offset(268, 0)
# 释放鼠标
action.release()
# 执行以上操作
action.perform()
time.sleep(1)
# 元素属性
locator = (By.CLASS_NAME,'layui-layer-content')
# 通过显示等待进行定位元素
WebDriverWait(driver, 20,0.5).until(EC.presence_of_element_located(locator))
# 获取元素属性值
text = driver.find_element_by_class_name('layui-layer-content').text
print(text)
assert text == '滑块验证通过'

After execution, we found that our pop-up text box has been positioned.

Summarize

This article briefly introduces how to complete the realization of the sliding box verification code through the mouse operation in python+selenium. After reading the article, you will find that it is actually very simple. It is achieved by positioning the sliding frame and sliding the offset. The knowledge points involved may be the positioning of a pseudo-element and the flashing text pop-up box. Well, thanks for reading, I hope it helps you.

Thanks to everyone who read my article carefully! ! !

I personally sorted out some technical materials I have compiled in my software testing career in the past few years, including: e-books, resume modules, various job templates, interview books, self-study projects, etc. Welcome everyone to click on the business card below to get it for free, don't miss it.

Guess you like

Origin blog.csdn.net/MXB1220/article/details/131926623