selenium自动化模拟解锁
1.滑动解锁
例1:https://www.helloweba.net/demo/2017/unlock/中的滑动解锁
代码如下:
slide-to-unlock-handle:表示滑块
slide-to-unlock-progress:滑过后的背景色
from time import sleep from selenium import webdriver from selenium.webdriver import ActionChains from selenium.common.exceptions import UnexpectedAlertPresentException driver = webdriver.Chrome() driver.get("https://www.helloweba.net/demo/2017/unlock/") # 定位滑块 slider = driver.find_element_by_class_name("slide-to-unlock-handle") action = ActionChains(driver) action.click_and_hold(slider).perform() # click_and_hold()单击并按下鼠标左键 for index in range(200): try: action.move_by_offset(5, 0).perform() # move_by_offset()移动鼠标,第一个参数为x坐标距离,第二个参数为y坐标距离 except UnexpectedAlertPresentException: break action.reset_actions() # 重置action sleep(0.1) # 等待停顿时间 # 打印警告框提示 success_text = driver.switch_to.alert.text print("success_text")
2.上下滑动选择
例2:上下滑动选择日期:http://www.jq22.com/yanshi4976
from time import sleep from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.jq22.com/yanshi4976") sleep(2) driver.switch_to_frame("iframe") driver.find_element_by_id("appDate").click() # 定位要滑动的年月日 dwwos = driver.find_element_by_class_name("dwwo") year = dwwos[0] month = dwwos[1] day = dwwos[2] action = webdriver.TouchActions(driver) action.scroll_from_element(year, 0, 5).perform() # scroll_from_element 滑动元素 action.scroll_from_element(month, 0, 30).perform() action.scroll_from_element(day, 0, 30).perform()