1. 实现目标
打开https://www.helloweba.net/demo/2017/unlock/,自动测试鼠标放置在滑块上,实现自动滑动。
2. 实现代码
其中,获得某个具体额滑动块,可找到所有class_name为slide-to-unlock-handle的,选择第0-第3项;
click_and_hold():单击并按下鼠标左键;
move_by_offset():移动鼠标,第一个参数为x坐标距离,第二个参数为y坐标距离;
reset_action():重置action。
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
driver = webdriver.Chrome()
driver.get("http://www.helloweba.com/demo/2017/unlock/")
# 定位滑动块
slider = driver.find_elements_by_class_name("slide-to-unlock-handle")[1]
action = ActionChains(driver)
action.click_and_hold(slider).perform()
for index in range(200):
try:
action.move_by_offset(2, 0).perform()