当前位置:网站首页>WEB自动化之键盘、鼠标操作
WEB自动化之键盘、鼠标操作
2022-08-02 14:15:00 【keer丶】
1.键盘相关操作
1.1导包
from selenium.webdriver.common.keys import Keys
在使用Keys时,需注意以下两点:
1.Keys必须通过ele.send_keys()使用,也就是必须要元素定位。
2.一个send_keys只能发布一次指令
1.2代码实战
实现步骤:打开百度搜索页—输入“自动化”—全选,剪切—输入“WEB”—黏贴,输入框中的内容应该是“WEB自动化”
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys #导包
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
ele_input = driver.find_element("id", "kw") #定位到搜索框
ele_input.send_keys("自动化") #向搜索框中输入“自动化”
time.sleep(2)
ele_input.send_keys(Keys.CONTROL+"A") #ctrl+a 全选
time.sleep(2)
ele_input.send_keys(Keys.CONTROL+"X") #ctrl+x 剪切
time.sleep(2)
ele_input.send_keys("WEB") #向搜索框中输入“WEB”
time.sleep(2)
ele_input.send_keys(Keys.CONTROL+"v") #ctrl+v 黏贴
time.sleep(2)
ele_input.send_keys(Keys.ENTER) #ctrl+enter 回车键搜索
time.sleep(2)
driver.quit()
2.鼠标相关操作
2.1导包
from selenium.webdriver.common.action_chains import ActionChains
2.2点击事件
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
ele_locator = driver.find_element("xpath","//a[@class='hot-title']")
ActionChains(driver).click(ele_locator).perform() #相当于ele_locator.click()
driver.close()
2.3右键点击
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
ele_locator = driver.find_element("xpath","//a[@class='hot-title']")
ActionChains(driver).context_click(ele_locator).perform() #右键点击content_click
driver.close()
2.4双击
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
ele_locator = driver.find_element("xpath","//a[@class='hot-title']")
ActionChains(driver).double_click(ele_locator).perform() #双击double_click
driver.close()
2.5推拽
注意:GUI中因为鼠标位置的原因,导致容易出问提,非GUI中比较可行
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("http://shop-xo.hctestedu.com/")
login_locator = WebDriverWait(driver,10).until(EC.visibility_of_element_located(("link text","登录")))
search_locator = WebDriverWait(driver,10).until(EC.visibility_of_element_located(("id","search-input")))
ActionChains(driver).drag_and_drop(login_locator,search_locator).perform() #drag_and_drop拖拽,把login_locator拖到search_locator
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("http://shop-xo.hctestedu.com/")
driver.maximize_window()
login_ele = WebDriverWait(driver,10).until(EC.visibility_of_element_located(("link text","登录")))
search_ele = WebDriverWait(driver,10).until(EC.visibility_of_element_located(("id","search-input")))
print(login_ele.location,login_ele.size) #{'x': 527, 'y': 7} {'height': 16, 'width': 24}
print(search_ele.location,search_ele.size) #{'x': 754, 'y': 72} {'height': 30, 'width': 308}
#也可以用偏移量drag_and_drop_by_offset(被移动目标,x偏移量,y偏移量)
ActionChains(driver).drag_and_drop_by_offset(login_ele,search_ele.location["x"]-login_ele.location["x"],search_ele.location["y"]-login_ele.location["y"]).perform()
2.6鼠标悬停
ActionChains(driver).move_to_element(driver.find_element("id","XX")).perform()
2.7滑动示例
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://passport.ctrip.com/user/reg/home")
driver.maximize_window()
driver.find_element("link text","同意并继续").click()
time.sleep(2) #设置等待时间等待页面元素加载完成
drop_btn = driver.find_element("class name","cpt-drop-btn")
slide_ele = driver.find_element("id","slideCode")
# print(drop_btn.location,drop_btn.size,slide_ele.size)
#click_and_hold(drop_btn)点击按压住drop_btn,move_by_offset(slide_ele.size["width"],0)滑动偏移量为slide_ele的宽(X轴)和0(Y轴)
ActionChains(driver).click_and_hold(drop_btn).move_by_offset(slide_ele.size["width"],0).perform()
time.sleep(2)
边栏推荐
猜你喜欢
随机推荐
【软件测试】用例篇
消息队列的技术选型
unity Domain Reload & scene Reload 静态变量重置
idea同时修改相同单词
【solidity智能合约基础】节约gas的利器--view和pure
锥形相位掩模的Talbot图像
2021-03-12
Evaluate multipath BBR congestion control on ns3
shader入门精要3
【软件测试】自动化测试selenium3
UnityAPI-Ray-Physics
Mobile copy constructor
Debug on pure method is called
Redis 学习part one
Oauth2.0 认证服务器搭建
Qt | 读取文件内容并删除文件 QFile
flex布局
代码细节带来的极致体验,ShardingSphere 5.1.0 性能提升密钥
golang的内存相关内容
HCIE学习记录——数通网络基础