from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
from time import sleep
driver = webdriver.Firefox() 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()
#平行移动大于解锁的长度的距离
try:
action.drag_and_drop_by_offset(dragger,500, 0).perform()
except UnexpectedAlertPresentException:
print("faild")
#等待2秒,方便捕捉弹框
sleep(2)
# 打印警告框提示
tt=driver.switch_to_alert()
success_text = tt.text
print(success_text)
#点击弹框中的ok按钮
tt.accept()
sleep(2)
driver.quit()