selenium+python自動化102-登入頁面滑動解鎖(ActionChains)
阿新 • • 發佈:2020-12-29
前言
登入頁面會遇到滑動解鎖,滑動解鎖的目的就是為了防止別人用程式碼登入(也就是為了防止你自動化登入),有些滑動解鎖是需要去拼圖這種會難一點。
有些直接拖到最最右側就可以了,本篇講下使用 selenium web 自動化的時候如何滑動解鎖。
滑動解鎖場景
看下圖,是我本地寫的一個 slider.html 網頁
除了輸入賬號和密碼,還需將滑塊拖動到最右端才能解鎖
最後才去點登陸按鈕
ActionChains 滑動滑塊
檢視 ActionChains 使用原始碼,相關介紹
ActionChains是一種自動化低階互動的方法,比如滑鼠移動、滑鼠按鈕操作、按鍵和上下文選單互動。這對於執行更復雜的操作(如懸停和拖放)非常有用
在呼叫ActionChains物件上的操作方法時,這些操作儲存在ActionChains物件的佇列中。呼叫perform()時,事件將按其順序激發排隊等候。
使用上有2種例項,一種可用於鏈模式
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
另外一種方式操作可以一個接一個排隊,然後執行
menu = driver.find_element_by_css_selector(".nav") hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1") actions = ActionChains(driver) actions.move_to_element(menu) actions.click(hidden_submenu) actions.perform()
不管是哪種方式,動作都是按照呼叫的順序執行的,一個接一個另一個。
實現程式碼
selenium 裡面滑動滑塊需用到滑鼠事件,回放下剛才操作的慢動作:按住 >> 按鈕 -> 往右移動滑鼠到最右端 -> 釋放滑鼠 -> 解鎖成功
於是會用到click_and_hold move_by_offset release 這三個方法,最後用 perform() 執行
from selenium import webdriver from selenium.webdriver import ActionChains driver = webdriver.Chrome() driver.get("file:///C:/Users/dell/Desktop/slider.html") driver.maximize_window() driver.find_element_by_id('id_username').send_keys("yoyo") driver.find_element_by_id('id_password').send_keys("123456") slider = driver.find_element_by_class_name("slider") # 滑塊解鎖 action = ActionChains(driver) action.click_and_hold(slider) # 按住 action.move_by_offset(248, 0) # 往右偏移248個畫素 action.release() # 釋放滑鼠 action.perform() # 執行 # 點登陸按鈕 # driver.find_element_by_xpath('//*[@type="submit"]').click()
具體拖動多少畫素,可以拖動滑鼠後看偏移量,如下圖 248px
ActionChains 相關原始碼
ActionChains 相關原始碼和使用說明
class ActionChains(object):
"""
一種方式操作可以一個接一個排隊,然後執行
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
另外一種方式操作可以一個接一個排隊,然後執行
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
不管是哪種方式,動作都是按照呼叫的順序執行的,一個接一個另一個。
"""
def __init__(self, driver):
def perform(self):
"""
執行所有的動作,放到最後
"""
def reset_actions(self):
"""
Clears actions that are already stored locally and on the remote end
"""
def click(self, on_element=None):
"""
點選元素
:Args:
- on_element: The element to click.
If None, clicks on current mouse position.
"""
def click_and_hold(self, on_element=None):
"""
按住滑鼠左鍵
:Args:
- on_element: The element to mouse down.
If None, clicks on current mouse position.
"""
def context_click(self, on_element=None):
"""
點選滑鼠右鍵
:Args:
- on_element: The element to context-click.
If None, clicks on current mouse position.
"""
def double_click(self, on_element=None):
"""
雙擊滑鼠
:Args:
- on_element: The element to double-click.
If None, clicks on current mouse position.
"""
def drag_and_drop(self, source, target):
"""
按住滑鼠左鍵在元素source上,然後拖動元素target位置並釋放滑鼠
:Args:
- source: The element to mouse down.
- target: The element to mouse up.
"""
def drag_and_drop_by_offset(self, source, xoffset, yoffset):
"""
在source元素上按住滑鼠左鍵,然後移動到目標偏移(相對source元素的偏移)並釋放滑鼠按鈕
:Args:
- source: The element to mouse down.
- xoffset: X offset to move to.
- yoffset: Y offset to move to.
"""
def key_down(self, value, element=None):
"""
同時按住幾個鍵不釋放,只能與修改鍵(Control、Alt和Shift)一起使用
:Args:
- value: The modifier key to send. Values are defined in `Keys` class.
- element: The element to send keys.
If None, sends a key to current focused element.
Example, pressing ctrl+c::
ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
"""
def key_up(self, value, element=None):
"""
釋放按住的鍵,跟上面一個相對應
:Args:
- value: The modifier key to send. Values are defined in Keys class.
- element: The element to send keys.
If None, sends a key to current focused element.
Example, pressing ctrl+c::
ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
"""
def move_by_offset(self, xoffset, yoffset):
"""
將滑鼠移動到距當前滑鼠位置的偏移位置。
:Args:
- xoffset: X offset to move to, as a positive or negative integer.
- yoffset: Y offset to move to, as a positive or negative integer.
"""
def move_to_element(self, to_element):
"""
將滑鼠移到元素的中間
:Args:
- to_element: The WebElement to move to.
"""
def move_to_element_with_offset(self, to_element, xoffset, yoffset):
"""
按指定元素的偏移量移動滑鼠。偏移相對於元素的左上角。
:Args:
- to_element: The WebElement to move to.
- xoffset: X offset to move to.
- yoffset: Y offset to move to.
"""
def pause(self, seconds):
""" 在指定的持續時間內暫停所有輸入(以秒為單位)"""
def release(self, on_element=None):
"""
在元素上釋放按住的滑鼠按鈕。
:Args:
- on_element: The element to mouse up.
If None, releases on current mouse position.
"""
def send_keys(self, *keys_to_send):
"""
將鍵傳送到當前聚焦元素。
:Args:
- keys_to_send: The keys to send. Modifier keys constants can be found in the
'Keys' class.
"""
def send_keys_to_element(self, element, *keys_to_send):
"""
向元素髮送鍵。
:Args:
- element: The element to send keys.
- keys_to_send: The keys to send. Modifier keys constants can be found in the
'Keys' class.
"""