欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > selenium的鼠标操作

selenium的鼠标操作

2025/3/12 18:01:53 来源:https://blog.csdn.net/wenyunick/article/details/146175127  浏览:    关键词: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 Bydriver = 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 sleepfrom selenium import webdriverfrom 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()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词