Selenium(9):模擬鼠標鍵盤的操作事件


鼠標操作事件

  在實際的web產品測試中,對於鼠標的操作,不單單只有click(),有時候還要用到右擊、雙擊、拖動等操作,這些操作包含在ActionChains類中。

ActionChains類中鼠標操作常用方法:

首先導入ActionChains類: from selenium.webdriver.common.action_chains import ActionChains

context_click():右擊
double_click():雙擊
drag_and_drop():拖動
move_to_element():鼠標移動到一個元素上
click_and_hold():按下鼠標左鍵在一個元素上(長按)

常用的鏈條命令

pause():停留、click():點擊、release():釋放、perform():執行

ActionChains(driver).move_to_element(元素對象).pause(秒).click(元素對象).release(元素對象).perform()

代碼如下:

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

current_path = os.path.dirname(os.path.abspath(__file__))  # 當前路徑
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路徑
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等

driver.get('https://www.baidu.com/')  # 打開網站

# 右擊操作 context_click()
element_obj = driver.find_element(By.XPATH,'//input[@id="su"]')  # 右擊百度一下
mouse_obj = ActionChains(driver)
mouse_obj.context_click(element_obj).perform()  # perform執行操作

# 點擊操作 click()
element_obj = driver.find_element(By.XPATH,'//a[text()="hao123"]')  
mouse_obj = ActionChains(driver)
mouse_obj.click(element_obj).release(element_obj).perform()  # 點擊hao123

# 長按操作 click_and_hold()
element_obj = driver.find_element(By.XPATH,'//a[text()="hao123"]')  
mouse_obj = ActionChains(driver)
mouse_obj.click_and_hold(element_obj).pause(10).release(element_obj).perform()  #長按 hao123 10秒后松開

# 鼠標移動到一個元素  move_to_element()
e1 = driver.find_element(By.XPATH,'//a[@name="tj_briicon"]')  
e2 = driver.find_element(By.XPATH,'//a[@name="tj_zhidao"]') 
mouse_obj = ActionChains(driver)
mouse_obj.move_to_element(e1).pause(3).click(e2).release(e2).perform()  # 鏈條命令  移動到 更多 元素上停頓3秒,然后點擊 知道 元素

 

 

鍵盤操作事件

  在實際的web測試工作中,需要配合鍵盤按鍵來操作,webdriver的keys()類提供鍵盤上所有按鍵的操作,還可以模擬組合鍵Ctrl_a,Ctrl+c/v等。

前置條件:導入Keys類

from selenium.webdriver.common.keys import Keys

頁面上的鍵盤操作(從搜索框中按兩下tab鍵)

driver.find_element(By.XPATH,'//input[@id="kw"]').click()
ActionChains(driver).send_keys(Keys.TAB).pause(1).send_keys(Keys.TAB).perform()

組合鍵操作 ctrl+a、ctrl+c、ctrl+v

driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

備注:

1、在使用修飾鍵的時候需要key_down()和key_up()方法

  修飾鍵包含ctrl、alt、shift

2、類似alt+F4 ctrl+alt+delete不能使用,這里的組合鍵只針對網頁生效的

代碼如下:

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

current_path = os.path.dirname(os.path.abspath(__file__))  # 當前路徑
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路徑
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等

driver.get('https://www.baidu.com/')  # 打開網站

# 頁面上的鍵盤操作   從搜索框中按兩下tab鍵
driver.find_element(By.XPATH,'//input[@id="kw"]').click()
ActionChains(driver).send_keys(Keys.TAB).pause(1).send_keys(Keys.TAB).perform()

# 組合鍵操作 ctrl+a、ctrl+c、ctrl+v、shift+a
# 修飾鍵 ctrl、alt、shift
# ctrl+a ==> 按下ctrl、按下a、松開a、松開ctrl
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

# 備注:系統級別的組合鍵不能使用,因為這里的組合鍵都是只針對網頁生效的

 


免責聲明!

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



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