ActionChains模擬用戶行為
很實用網(wǎng)址:https://www.cnblogs.com/lxbmaomao/p/10389786.html
簡介:講解使用selenium里面的ActionChains模擬用戶的行為
需求:
需要模擬鼠標(biāo)操作才能進行的情況何乎,比如單擊欺殿、雙擊、點擊鼠標(biāo)右鍵、拖拽
解決:selenium提供了一個類來處理這類事件
selenium.webdriver.common.action_chains.ActionChains(driver)
腳本:
from selenium.webdriver.common.action_chains import ActionChains
執(zhí)行原理:
調(diào)用ActionChains的方法時不會立即執(zhí)行,會將所有的操作按順序存放在一個隊列里迫卢,當(dāng)調(diào)用perform()方法時辛辨,隊列中的事件會依次執(zhí)行
支持鏈?zhǔn)綄懛ɑ蛘叻植綄懛?br> ActionChains(driver).click(ele).perform()
鼠標(biāo)和鍵盤方法列表:
perform() 執(zhí)行鏈中的所有動作
click(on_element=None) 單擊鼠標(biāo)左鍵
context_click(on_element=None) 點擊鼠標(biāo)右鍵
double_click(on_element=None) 雙擊鼠標(biāo)左鍵
move_to_element(to_element) 鼠標(biāo)移動到某個元素
ele.send_keys(keys_to_send) 發(fā)送某個詞到當(dāng)前焦點的元素
========== 不常用 ==========
click_and_hold(on_element=None) 點擊鼠標(biāo)左鍵瓮增,不松開
release(on_element=None) 在某個元素位置松開鼠標(biāo)左鍵
key_down(value, element=None) 按下某個鍵盤上的鍵
key_up(value, element=None) 松開某個鍵
drag_and_drop(source, target) 拖拽到某個元素然后松開
drag_and_drop_by_offset(source, xoffset, yoffset) 拖拽到某個坐標(biāo)然后松開
move_by_offset(xoffset, yoffset) 鼠標(biāo)從當(dāng)前位置移動到某個坐標(biāo)
move_to_element_with_offset(to_element, xoffset, yoffset) 移動到距某個元素(左上角坐標(biāo))多少距離的位置
send_keys_to_element(element, keys_to_send) 發(fā)送某個鍵到指定元素
練習(xí)
鼠標(biāo)事件實戰(zhàn)之hover菜單欄彈出
簡介:鼠標(biāo)事件之菜單欄hover彈出
1别厘、#引入 ActionChains 類
from selenium.webdriver.common.action_chains import ActionChains
2虱饿、move_to_element(to_element) 鼠標(biāo)移動到某個元素
對定位到的元素執(zhí)行鼠標(biāo)移動到上面的操作
ActionChains(driver).move_to_element(ele1).perform()
from selenium import webdriver
import time
# 引入 ActionChains 類
from selenium.webdriver.common.action_chains import ActionChains
#拿到一輛小汽車driver
driver = webdriver.Firefox()
#跳轉(zhuǎn)網(wǎng)頁
driver.get("https://xdclass.net")
print(driver.title)
time.sleep(3)
#定位鼠標(biāo)移動到上面的元素
menu_ele = driver.find_element_by_css_selector(".list_item > li:nth-child(1)")
ActionChains(driver).move_to_element(menu_ele).perform()
#選中子菜單
sub_menu_ele = driver.find_element_by_css_selector(".base > div:nth-child(3) > a:nth-child(1)")
time.sleep(2)
sub_menu_ele.click()
小試牛刀
from selenium import webdriver
import time
# 引入 ActionChains 類
from selenium.webdriver.common.action_chains import ActionChains
#拿到一輛小汽車driver
driver = webdriver.Firefox()
#跳轉(zhuǎn)網(wǎng)頁
driver.get("https://xdclass.net")
print(driver.title)
time.sleep(3)
#獲取登錄框拥诡,只是獲取到触趴,沒有別的操作
login_ele = driver.find_element_by_css_selector(".login > span:nth-child(2)")
#觸發(fā)點擊事件
ActionChains(driver).click(login_ele).perform()
#查找輸入框,輸入賬號密碼渴肉,輸入框需要提前清理
driver.find_element_by_css_selector(".mobienum > input:nth-child(1)").clear()
driver.find_element_by_css_selector(".mobienum > input:nth-child(1)").send_keys("177xxxx")
driver.find_element_by_css_selector(".psw > input:nth-child(1)").clear()
driver.find_element_by_css_selector(".psw > input:nth-child(1)").send_keys("xxxx")
#查找登錄按鈕
login_btn_ele = driver.find_element_by_css_selector(".btn").click()
#判斷是否登錄成功冗懦,鼠標(biāo)移動上面,判斷彈窗字符
user_info_ele = driver.find_element_by_css_selector(".avatar_img")
time.sleep(1)
#觸發(fā)hover事件
ActionChains(driver).move_to_element(user_info_ele).perform()
#獲取用戶名的元素
user_name_ele = driver.find_element_by_css_selector(".username")
print("====測試結(jié)果====")
print(user_name_ele.text)
name = user_name_ele.text
if name == "測試賬號":
print("login ok")
else:
print("login fail")