在webdriver 中,鼠標(biāo)操作都封裝在ActionChains類中,使用的時(shí)候需要導(dǎo)入這個(gè)包。
from selenium.webdriver import ActionChains
ActionChains方法列表如下:
click(on_element=None) ——單擊鼠標(biāo)左鍵
click_and_hold(on_element=None) ——點(diǎn)擊鼠標(biāo)左鍵,不松開
context_click(on_element=None) ——點(diǎn)擊鼠標(biāo)右鍵
double_click(on_element=None) ——雙擊鼠標(biāo)左鍵
drag_and_drop(source, target) ——拖拽到某個(gè)元素然后松開
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某個(gè)坐標(biāo)然后松開
key_down(value, element=None) ——按下某個(gè)鍵盤上的鍵
key_up(value, element=None) ——松開某個(gè)鍵
move_by_offset(xoffset, yoffset) ——鼠標(biāo)從當(dāng)前位置移動(dòng)到某個(gè)坐標(biāo)
move_to_element(to_element) ——鼠標(biāo)移動(dòng)到某個(gè)元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移動(dòng)到距某個(gè)元素(左上角坐標(biāo))多少距離的位置
perform() ——執(zhí)行鏈中的所有動(dòng)作
release(on_element=None) ——在某個(gè)元素位置松開鼠標(biāo)左鍵
send_keys(*keys_to_send) ——發(fā)送某個(gè)鍵到當(dāng)前焦點(diǎn)的元素
send_keys_to_element(element, *keys_to_send) ——發(fā)送某個(gè)鍵到指定元素
1.單擊左鍵+單擊右鍵+左鍵雙擊的示例用法。
# @Author : 小紅牛
# 微信公眾號:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/clicks.htm')
click_btn = driver.find_element(By.XPATH, '//input[@value="click me"]')
# 雙擊按鈕
doubleclick_btn = driver.find_element(By.XPATH, '//input[@value="dbl click me"]')
# 右鍵單擊按鈕
rightclick_btn = driver.find_element(By.XPATH, '//input[@value="right click me"]')
action = ActionChains(driver)
# 鏈?zhǔn)秸{(diào)用
action.click(click_btn).double_click(doubleclick_btn).context_click(rightclick_btn).perform()
print(driver.find_element(By.NAME, 't2').get_attribute('value'))
# [CLICK][DOUBLE_CLICK][RIGHT_CLICK]
sleep(5)
driver.quit()
2.模擬鼠標(biāo)拖動(dòng)操作,該操作有兩個(gè)必要參數(shù),source:鼠標(biāo)拖動(dòng)的元素,target:鼠標(biāo)拖至并釋放的目標(biāo)元素。
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/dragDropMooTools.htm')
dragger = driver.find_element(By.ID, 'dragger') # 被拖拽元素
item1 = driver.find_element(By.XPATH, '//div[text()="Item 1"]') # 目標(biāo)元素1
item2 = driver.find_element(By.XPATH, '//div[text()="Item 2"]') # 目標(biāo)2
item3 = driver.find_element(By.XPATH, '//div[text()="Item 3"]') # 目標(biāo)3
item4 = driver.find_element(By.XPATH, '//div[text()="Item 4"]') # 目標(biāo)4
action = ActionChains(driver)
action.drag_and_drop(dragger, item1).perform() # 1.移動(dòng)dragger到目標(biāo)1
sleep(2)
action.click_and_hold(dragger).release(item2).perform() # 2.效果與上句相同,也能起到移動(dòng)效果
sleep(2)
action.click_and_hold(dragger).move_to_element(item3).release().perform() # 3.效果與上兩句相同,也能起到移動(dòng)的效果
sleep(2)
# action.drag_and_drop_by_offset(dragger, 400, 150).perform() # 4.移動(dòng)到指定坐標(biāo)
action.click_and_hold(dragger).move_by_offset(400, 150).release().perform() # 5.與上一句相同,移動(dòng)到指定坐標(biāo)
sleep(5)
# 關(guān)閉瀏覽器
driver.close()
3.move_to_element()鼠標(biāo)懸停,當(dāng)你把鼠標(biāo)放在,百度更多位置時(shí),就會(huì)出現(xiàn)更多的功能,這就是一個(gè)懸停的效果,選擇里面的翻譯進(jìn)行點(diǎn)擊操作。如下圖所示
更多的網(wǎng)頁元素結(jié)構(gòu),里面有個(gè)name="tj_briicon"屬性,定位元素方法有很多,選擇一個(gè)就可以了。
<a href="http://www.baidu.com/more/" name="tj_briicon" class="s-bri c-font-normal c-color-t" target="_blank">更多</a>
翻譯的網(wǎng)頁元素結(jié)構(gòu),這里可使用By.LINK_TEXT去定位元素
<a class="img-spacing" href="http://fanyi.baidu.com/" target="_blank" name="tj_fanyi"><img src="https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newfanyi-da0cea8f7e.png"><div class="s-top-more-title c-font-normal c-color-t">翻譯</div></a>
具體實(shí)現(xiàn)代碼
# @Author : 小紅牛
# 微信公眾號:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# 1.定位更多的元素位置
element = driver.find_element(By.NAME, 'tj_briicon')
# 實(shí)例化鼠標(biāo)
action = ActionChains(driver)
# 鼠標(biāo)懸停在百度更多
action.move_to_element(element)
# 執(zhí)行鼠標(biāo)事件
action.perform()
# 2.選擇更多里面的翻譯
href = driver.find_element(By.LINK_TEXT, '翻譯')
# print(href.text) # 翻譯
href.click()
sleep(5)
driver.quit()
4.key_down+key_up的示例用法:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://sahitest.com/demo/keypress.htm')
key_up_radio = driver.find_element(By.ID, 'r1') # 監(jiān)測按鍵升起
key_down_radio = driver.find_element(By.ID, 'r2') # 監(jiān)測按鍵按下
key_press_radio = driver.find_element(By.ID, 'r3') # 監(jiān)測按鍵按下升起
enter = driver.find_elements(By.XPATH, '//form[@name="f1"]/input')[1] # 輸入框
result = driver.find_elements(By.XPATH, '//form[@name="f1"]/input')[0] # 監(jiān)測結(jié)果
# 監(jiān)測key_down
key_down_radio.click()
ActionChains(driver).key_down(Keys.CONTROL, enter).key_up(Keys.CONTROL).perform()
print(result.get_attribute('value'))
# key downed charCode=[0] keyCode=[17] CTRL
sleep(2)
# 監(jiān)測key_up
key_up_radio.click()
enter.click()
ActionChains(driver).key_down(Keys.SHIFT).key_up(Keys.SHIFT).perform()
print(result.get_attribute('value'))
# key upped charCode=[0] keyCode=[16] NONE
sleep(2)
# 監(jiān)測key_press
key_press_radio.click()
enter.click()
ActionChains(driver).send_keys('a').perform()
print(result.get_attribute('value'))
# key pressed charCode=[97] keyCode=[97] NONE
sleep(5)
driver.quit()
5.鍵盤操作,鍵盤對應(yīng)的方法在Keys類中。Keys 類幾乎提供了鍵盤上的所有按鍵方法,我們可以使用 send_keys + Keys 實(shí)現(xiàn)輸出鍵盤上的組合按鍵如 “Ctrl + C”、“Ctrl + V” 等。
from selenium.webdriver.common.keys import Keys
# 單鍵
element.send_keys(Keys.XXX)
# 組合鍵
element.send_keys(Keys.XXX, 'a') # 注意這里的組合鍵都是小寫
百度搜索李白的示例鍵盤用法
# @Author : 小紅牛
# 微信公眾號:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
input_ele = driver.find_element(By.ID, 'kw')
input_ele.send_keys('李白8')
sleep(1)
# 等價(jià)于BACKSPACE(刪除)
input_ele.send_keys(Keys.BACK_SPACE)
sleep(1)
# 組合鍵Ctrl+a全選
input_ele.send_keys(Keys.CONTROL, 'a')
sleep(1)
# 剪切
input_ele.send_keys(Keys.CONTROL, 'x')
sleep(1)
# 粘貼
input_ele.send_keys(Keys.CONTROL, 'v')
# 回車
input_ele.send_keys(Keys.ENTER)
sleep(5)
driver.quit()
6.常用的鍵盤事件:
NULL = '\ue000'
CANCEL = '\ue001' # ^break
HELP = '\ue002'
BACKSPACE = '\ue003'
BACK_SPACE = BACKSPACE #刪除鍵
TAB = '\ue004' #TAB鍵
CLEAR = '\ue005'
RETURN = '\ue006'
ENTER = '\ue007' #回車鍵
SHIFT = '\ue008' #Shift鍵
LEFT_SHIFT = SHIFT
CONTROL = '\ue009'
LEFT_CONTROL = CONTROL #Ctrl 鍵
ALT = '\ue00a' #Alt 鍵
LEFT_ALT = ALT
PAUSE = '\ue00b'
ESCAPE = '\ue00c' #ECS鍵
SPACE = '\ue00d' #空格鍵
PAGE_UP = '\ue00e' #PgUp 鍵
PAGE_DOWN = '\ue00f' #PgDwon 鍵
END = '\ue010' #END 鍵
HOME = '\ue011' #HOME 鍵
LEFT = '\ue012' #左鍵
ARROW_LEFT = LEFT
UP = '\ue013' #上鍵
ARROW_UP = UP
RIGHT = '\ue014'
ARROW_RIGHT = RIGHT #右鍵
DOWN = '\ue015' #下鍵
ARROW_DOWN = DOWN
INSERT = '\ue016' #insert鍵
DELETE = '\ue017' #del鍵
SEMICOLON = '\ue018' #';'鍵
EQUALS = '\ue019' #'='鍵
#數(shù)字鍵盤
NUMPAD0 = '\ue01a' # number pad keys
NUMPAD1 = '\ue01b'
NUMPAD2 = '\ue01c'
NUMPAD3 = '\ue01d'
NUMPAD4 = '\ue01e'
NUMPAD5 = '\ue01f'
NUMPAD6 = '\ue020'
NUMPAD7 = '\ue021'
NUMPAD8 = '\ue022'
NUMPAD9 = '\ue023'
MULTIPLY = '\ue024' # '*' 鍵
ADD = '\ue025' # '+' 鍵
SEPARATOR = '\ue026' #','鍵
SUBTRACT = '\ue027' # '-' 鍵
DECIMAL = '\ue028' # '.'鍵
DIVIDE = '\ue029' #'/'鍵
F1 = '\ue031' # function keys
F2 = '\ue032'
F3 = '\ue033'
F4 = '\ue034'
F5 = '\ue035'
F6 = '\ue036'
F7 = '\ue037'
F8 = '\ue038'
F9 = '\ue039'
F10 = '\ue03a'
F11 = '\ue03b'
F12 = '\ue03c'
META = '\ue03d'
COMMAND = '\ue03d'
完畢??!感謝您的收看文章來源:http://www.zghlxwxcb.cn/news/detail-776133.html
----------★★歷史博文集合★★----------
我的零基礎(chǔ)Python教程,Python入門篇 進(jìn)階篇 視頻教程 Py安裝py項(xiàng)目 Python模塊 Python爬蟲 Json Xpath 正則表達(dá)式 Selenium Etree CssGui程序開發(fā) Tkinter Pyqt5 列表元組字典數(shù)據(jù)可視化 matplotlib 詞云圖 Pyecharts 海龜畫圖 Pandas Bug處理 電腦小知識office自動(dòng)化辦公 編程工具文章來源地址http://www.zghlxwxcb.cn/news/detail-776133.html
到了這里,關(guān)于Selenium教程04:鼠標(biāo)+鍵盤網(wǎng)頁的模擬操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!