接虫师的http://www.cnblogs.com/fnng/p/7235498.html。自己设计了一个方法,学习了。
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/")
sleep(2)
dragger = driver.find_elements_by_class_name('slide-to-unlock-handle')[0]
actions = ActionChains(driver)
# actions.click_and_hold(dragger)
for index in range(100):
try:
actions.drag_and_drop_by_offset(dragger, 500, 0).perform()
# 将移动的横坐标设置很大,大于滑动边框的距离,使用drag_and_drop直接移动
except UnexpectedAlertPresentException:
break
text1 = driver.switch_to.alert.text
print(text1)
driver.quit()