web自動化中,有一些地方需要用到鼠標操作,比如拖動,鼠標懸停,右擊等操作,那么selenium如何操作這些方法呢?在寫appium的時候也經常使用這個,其實這兩個方法是相似的,appium中TouchAction,那么selenium中是什么呢?
ActionChains
ActionChains表示selenium的鼠標操作的庫,使用前一定要進行導入庫 from selenium.webdriver.common.action_chains import ActionChains
ActionChains中支持,右擊,雙擊,懸停,以及拖放等操作。
具體的操作內容,我們可以查看ActionChains源代碼進行查看(按住ctrl點擊ActionChains)
操作方法: ActionChains(driver).XXXXX.perform() 其中xxxx表示一些鼠標操作的方法,然后perform進行執行所有ActionChains存儲的操作內容
鼠標右擊
方法: context_click() 模擬鼠標右擊操作
源碼:
def context_click(self, on_element=None): """ Performs a context-click (right click) on an element. """ if on_element: self.move_to_element(on_element) if self._driver.w3c: self.w3c_actions.pointer_action.context_click() self.w3c_actions.key_action.pause() self.w3c_actions.key_action.pause() else: self._actions.append(lambda: self._driver.execute( Command.CLICK, {'button': 2})) return self
通過百度搜索框進行右擊查看內容
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome() driver.get('https://www.baidu.com') element = driver.find_element_by_id('kw') time.sleep(1) # 通過perform執行ActionChains中存儲的操作 ActionChains(driver).context_click(element).perform()
鼠標雙擊
方法: double_click() 用來模擬鼠標的雙擊操作
源碼:
def double_click(self, on_element=None): """ Double-clicks an element. """ if on_element: self.move_to_element(on_element) if self._driver.w3c: self.w3c_actions.pointer_action.double_click() for _ in range(4): self.w3c_actions.key_action.pause() else: self._actions.append(lambda: self._driver.execute( Command.DOUBLE_CLICK, {})) return self
這里安靜沒有做演示,只有通過定位元素進行描述如何操作方法內容
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome() driver.get('https://www.baidu.com') element = driver.find_element_by_id('kw') time.sleep(1) # 鼠標雙擊 ActionChains(driver).double_click(element).perform()
鼠標懸停
web頁面上存在一些鼠標放上去就會顯示一些內容,這個時候就可以用到鼠標懸停操作了
方法: move_to_element()
源代碼:
def move_to_element(self, to_element): """ Moving the mouse to the middle of an element. """ if self._driver.w3c: self.w3c_actions.pointer_action.move_to(to_element) self.w3c_actions.key_action.pause() else: self._actions.append(lambda: self._driver.execute( Command.MOVE_TO, {'element': to_element.id})) return self
通過進入百度首頁,進行對設置元素進行懸停操作
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome() driver.get('https://www.baidu.com') element = driver.find_element_by_id('s-usersetting-top') time.sleep(1) # 鼠標懸停 ActionChains(driver).move_to_element(element).perform()
鼠標拖放
通過鼠標在一個元素位置拖到另一個元素位置。
方法: drag_and_drop()
源碼:
def drag_and_drop(self, source, target): """ Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button. :Args: - source: The element to mouse down. - target: The element to mouse up. """ self.click_and_hold(source) self.release(target) return self
這里安靜也是通過代碼示例進行描述的,具體操作可以找實例進行來操作
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome() driver.get('https://www.baidu.com') element = driver.find_element_by_id('s-usersetting-top') time.sleep(1) e2 = driver.find_element_by_class_name('show-city') # 鼠標拖動,這里需要填寫2個元素位置,第一個為需要移動的,第2個位移動到元素 ActionChains(driver).drag_and_drop(element,e2).perform()
其他操作
看到這里了,會發現,其實使用方法都是一樣的,只是一些操作方法名不同,安靜就不一一進行寫了。
方法 | 說明 |
click_and_hold() | 單機鼠標左鍵不放 |
move_to_element_with_offset() | 移動到特定位置(坐標,瀏覽器左上角開始) |
release() |
釋放鼠標 |
這一章沒有具體的實例來操作這些鼠標方法,大家可以自己手動試下公司的項目或者找一些網站進行操作
如果安靜寫的文章如果對您有幫助,點個關注,持續更新。不懂的或者寫錯的地方,可以下方留言。