日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解

發布時間:2024/7/23 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引言

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

舉例:

#cording=gbk

import os

import time

from selenium import webdriver

from selenium.webdriver.common.by import By #導入by方法

from selenium.webdriver.common.action_chains import ActionChains ##對鼠標事件操作

from selenium.webdriver.common.keys import Keys # 對鍵盤事件操作

current_path=os.path.dirname(__file__)

firefox_path=current_path+"/../webdriver/geckodriver.exe"

driver=webdriver.Firefox(executable_path=firefox_path)

driver.get("http://www.baidu.com")

# 先輸入百度

driver.find_element_by_id('kw').send_keys('百度')

time.sleep(3)

# 1.刪除度

driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)

time.sleep(3)

#2.清空輸入框,重新輸入值

driver.find_element_by_id('kw').clear()

driver.find_element_by_id('kw').send_keys('安琪兒')

time.sleep(5)

# 3.ctrl+a 全選輸入框里的內容

driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')

time.sleep(3)

# 4.ctrl+x 剪切輸入框里的內容

driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')

time.sleep(3)

# 5. ctrl+v 粘貼剪切的內容

driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'v')

time.sleep(3)

# 6. 回車

driver.find_element_by_id('su').send_keys(Keys.ENTER)

time.sleep(3)

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

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

context_click() :右擊

double_click() :雙擊

drag_and_drop() :拖動

move_to_element() :鼠標移動到一個元素上

舉例:

#cording=gbk

import os

from selenium import webdriver

from selenium.webdriver.common.by import By #導入by方法

from selenium.webdriver.common.action_chains import ActionChains ##對鼠標事件操作

current_path=os.path.dirname(__file__)

firefox_path=current_path+"/../webdriver/geckodriver.exe"

driver=webdriver.Firefox(executable_path=firefox_path)

driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby9teS5odG1s.html")

mouse=ActionChains(driver) #創建一個鼠標對象

# element1=driver.find_element(By.XPATH,"//img[@src='/zentao/theme/default/images/main/zt-logo.png']") #Xpath利用屬性定位

element1=driver.find_element(By.XPATH,"//img[contains(@src,'images/main/zt-logo.png')]") #xpath使用包含屬性方法定位

mouse.context_click(element1).perform() #執行鼠標右擊,.perform() 表示執行

element2=driver.find_element(By.XPATH,"//button[@type='button' and @class='btn' ]") #多屬性定位

mouse.move_to_element(element2).perform() #移動到這個元素上

#對元素進行截圖

driver.find_element(By.XPATH,"//button[@id='submit'][@type='submit']").screensh

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持龍方網絡。

總結

以上是生活随笔為你收集整理的python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。