selenium+python自動化102-登錄頁面滑動解鎖(ActionChains)


前言

登錄頁面會遇到滑動解鎖,滑動解鎖的目的就是為了防止別人用代碼登錄(也就是為了防止你自動化登錄),有些滑動解鎖是需要去拼圖這種會難一點。
有些直接拖到最最右側就可以了,本篇講下使用 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
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/


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.
        """


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM