selenium的鼠标操作

1、鼠标操作

鼠标时间对应的方法在那个类中?

ActionChains类,实例化 鼠标对象

1、context_click(element) # 右击

2、double_click(element) #双击
3、double_and_drop(source, target) # 拖拽
4、move_to_element(element) # 悬停 【重点】
5、perform() # 执行以上事件的方法 【重点】

复制代码
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('https://www.baidu.com/')
element=driver.find_element(By.ID,'kw')
# 实例化 鼠标对象
action=ActionChains(driver)
#鼠标右键,执行鼠标右键点击(上下文菜单)
#action.context_click(element)
#鼠标双击
#action.double_click(element)
elementNews=driver.find_element(By.LINK_TEXT,"新闻")
#点击鼠标左键并保持按下状态
#action.click_and_hold(elementNews)
#新闻那个按钮保持按下状态,蓝色状态
#点击鼠标左键,就直接进入新闻页面
#action.click(elementNews)
#鼠标悬停,鼠标停在新闻那个地方,蓝色状态
action.move_to_element(elementNews)

# 鼠标执行操作!!!不执行没效果
action.perform()
sleep(3)
driver.close()

2、等待

1、为什么要设置元素等待

​ 由于电脑配置或网络原因,在查找元素时,元素代码未在第一时间内被加载出来,而抛出未找到元素异常。

2、什么是元素等待

​ 元素在第一次未找到时,元素等待设置的时长被激活,如果在设置的有效时长内找到元素,继续执行代码,如果超出设置的时长未找打元素,抛出未找到元素异常。

3、元素等待分类

​ 隐式等待:针对全局元素生效;(讲这个)

​ 显示等待:稍微麻烦,有兴趣的可以下去了解,他是针对单个元素生效。

driver.implicitly_wait(30) # 一般情况下设置30秒

解释:

​ 隐式等待针对所有元素生效。 2. 一般情况下为前置必写代码(1.获取浏览器驱动对象;2. 最大化浏览器;3. 设置隐式等待)

参考:自动化测试------selenium(完结篇)_selenium自动化测试-CSDN博客

复制代码
from time import sleep

from selenium import webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# 设置正确的驱动路径

driver = webdriver.Chrome()
# 2、窗口最大化
driver.maximize_window()
# 3、设置隐式等待
driver.implicitly_wait(30)
# 打开网页
driver.get("https://baidu.com")


# 定位输入框
input_element = driver.find_element(By.ID, 'kw')

# 输入内容并发送组合键
input_element.send_keys("Hello, World!")
sleep(3)
input_element.send_keys(Keys.COMMAND, 'a')  # 全选
sleep(3)
input_element.send_keys(Keys.COMMAND, 'c')  # 复制
sleep(3)
input_element.send_keys(Keys.COMMAND, 'v')  # 粘贴
sleep(3)
# 关闭浏览器
driver.quit()
相关推荐
青草地溪水旁10 小时前
tcpdump调试
网络·测试工具·tcpdump
深蓝电商API2 天前
实战破解前端渲染:当 Requests 无法获取数据时(Selenium/Playwright 入门)
前端·python·selenium·playwright
卓码软件测评2 天前
第三方软件验收测试:【AutoIt与Selenium结合测试文件上传/下载等Windows对话框】
windows·功能测试·selenium·测试工具·性能优化·可用性测试
最好的我们!2 天前
解决selenium的EdgeOptions addArguments is not supported问题
selenium·测试工具
万粉变现经纪人3 天前
如何解决 pip install 安装报错 ImportError: cannot import name ‘xxx’ from ‘yyy’ 问题
python·selenium·测试工具·flask·scikit-learn·fastapi·pip
gc_22993 天前
学习Python中Selenium模块的基本用法(18:使用ActionChains操作鼠标)
python·selenium
卓码软件测评3 天前
第三方软件登记测试机构:【软件登记测试机构HTML5测试技术】
前端·功能测试·测试工具·html·测试用例·html5
可可南木3 天前
ICT 数字测试原理 3 --SAFETYGUARD 文件
开发语言·测试工具·pcb工艺
卓码软件测评3 天前
第三方软件测试公司:【Gatling基于Scala的开源高性能负载测试工具】
测试工具·开源·scala·压力测试·可用性测试·第三方软件测试
paid槮4 天前
selenium完整版一览
selenium·测试工具