Backend Development
Python Tutorial
An example analysis of how to use Selenium to simulate JQuery sliding unlock in PythonAn example analysis of how to use Selenium to simulate JQuery sliding unlock in Python
This article mainly introduces the Selenium simulation JQuery sliding unlocking example in Python. It has certain reference value. Interested friends can refer to it.
This article introduces the Selenium simulation JQuery sliding unlocking example in Python. , share it with everyone, and leave a note for yourself
Sliding unlocking has always been one of the difficulties in UI automation. I will add an example of sliding unlocking, hoping to give some ideas to students who are new to Web UI automation testing. .
First let’s look at an example.

When I manually click the slider, all that changes is the style:
1. slide-to-unlock-handle represents the slider. The left margin is getting bigger (because it is moving to the right!)
2. Slide-tounlock-progress means that the background is yellow after sliding over. The width of the yellow is increasing because the places where the slide passes turn yellow. .
Except for these, there are no other changes, so it seems that we can’t use mouse dragging! Because mouse dragging moves one element to another element. Like this:
# 定位元素的原位置
element = driver.find_element_by_id("xx")
# 定位元素要移动到的目标位置
target = driver.find_element_by_id("xx")
ActionChains(driver).drag_and_drop(element, target).perform()But during my manual demonstration, the position of the element did not change.
Let’s see how I achieved it.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.helloweba.com/demo/2017/unlock/")
dragger = driver.find_elements_by_class_name("slide-to-unlock-handle")[0]
action = ActionChains(driver)
action.click_and_hold(dragger).perform() #鼠标左键按下不放
for index in range(200):
try:
action.move_by_offset(2, 0).perform() #平行移动鼠标
except UnexpectedAlertPresentException:
break
action.reset_actions()
sleep(0.1) #等待停顿时间
# 打印警告框提示
success_text = driver.switch_to.alert.text
print(success_text)
sleep(5)
driver.quit()driver.find_elements_by_class_name("slide-to-unlock-handle")[0]
First, I want to operate There are several sliders on the page. I first find the first one among them through the class attribute.
click_and_hold()
Press the left mouse button on the slider through the click_and_hold() method.
move_by_offset()
The next step is to move the position of the slider through the for loop. The first parameter of the move_by_offset() method is the X axis, and the second parameter is Y-axis, unit is pixel. Because it is a parallel movement, Y is set to 0. X moves two pixels at a time.
When the unlock is successful, UnexpectedAlertPresentException will be thrown, and the loop will be jumped out after catching it.
Each cycle sleeps for 0.1 seconds. The smaller the time interval, the smoother the movement!
Now that the core steps have been introduced, the next step is to get the prompt information on the warning box and print it, and then close the browser.
The print result is:
successfully unlock!
The above is the detailed content of An example analysis of how to use Selenium to simulate JQuery sliding unlock in Python. For more information, please follow other related articles on the PHP Chinese website!
Python and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AMTo maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.
Python: Games, GUIs, and MoreApr 13, 2025 am 12:14 AMPython excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.
Python vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AMPython is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.
The 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AMYou can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.
Python: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AMPython is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PMYou can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.
How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AMHow to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...
How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AMHow to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software





