selenium 滑動解鎖(drag_and_drop_by_offset)
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()
selenium 滑動解鎖(drag_and_drop_by_offset)