登录|注册|帮助中心|联系我们

导航
首页 综合百科 生活常识 数码科技 明星名人 传统文化 互联网 健康 影视 美食 教育 旅游 汽车 职场 时尚 运动 游戏 家电 地理 房产 金融 节日 服饰 乐器 歌曲 动物 植物
当前位置:首页 > 数码科技

鼠标连点测试(连点器速度测试)

发布时间:2023年1月5日责任编辑:周小新标签:测试鼠标

鼠标表示鼠标事件。鼠标操作是通过使用低级界面执行的,该界面允许我们向Web浏览器提供虚拟化的设备输入操作。

鼠标动作操作方法详细介绍如下:

click_and_hold
?

移动到该元素,然后在给定元素的中间单击(不释放)

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.baidu.com/\\\') ???time.sleep(2) ???setting = driver.find_element_by_xpath(\\\'//*[@ and text()="设置"]\\\') ???ActionChains(driver).click_and_hold(setting).perform() ???time.sleep(5) context_click
?

首先将鼠标移动到元素的位置,然后在给定元素上执行上下文单击(右键单击)

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.runoob.com/python/python-tutorial.html\\\') ???time.sleep(2) ???setting = driver.find_element_by_xpath(\\\'//*[@rel="noopener noreferrer" and text()="Python 3.X 版本的教程"]\\\') ???ActionChains(driver).context_click(setting).perform() ???time.sleep(5) double_click
?

移动到该元素,并在给定元素的中间双击

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.runoob.com/python/python-tutorial.html\\\') ???time.sleep(2) ???setting = driver.find_element_by_xpath(\\\'//*[@rel="noopener noreferrer" and text()="Python 3.X 版本的教程"]\\\') ???ActionChains(driver).double_click(setting).perform() ???time.sleep(5) move_to_element
?

将鼠标移到元素的中间。执行此操作时,该元素也会滚动到视图中,再进行定位操作

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.baidu.com/\\\') ???setting = driver.find_element_by_xpath(\\\'//*[@ and @name="tj_settingicon"]\\\') ???ActionChains(driver).move_to_element(setting).perform() ???driver.find_element_by_xpath(\\\'//*[text()="搜索设置"]\\\').click() ???time.sleep(5) ??? move_by_offset
?

将鼠标从其当前位置(或0,0)移动给定的偏移量。如果坐标在视图窗口之外,则鼠标将最终在浏览器窗口之外

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.baidu.com/\\\') ???setting = driver.find_element_by_xpath(\\\'//*[@ and @name="tj_settingicon"]\\\') ???ActionChains(driver).move_to_element(setting).perform() ???time.sleep(5) ???xOffset = 1 ???yOffset = 1 ???webdriver.ActionChains(driver).move_by_offset(xOffset, yOffset).perform() ???driver.find_element_by_xpath(\\\'//*[text()="搜索设置"]\\\').click() ???time.sleep(5) ???

如我们把xOffset和yOffset的值稍微设置大一点,设置值为 100,就会报不在范围的错误:Message: move target out of bounds

/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.pyTraceback (most recent call last): ?File "/Users/lifeng/python-virtualenv/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response ???raise exception_class(message, screen, stacktrace)selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds ?(Session info: chrome=89.0.4389.82) ? drag_and_drop
?

首先在源元素上单击并按住,然后移动到目标元素的位置,然后释放鼠标

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.baidu.com/\\\') ???setting = driver.find_element_by_xpath(\\\'//*[@ and @name="tj_settingicon"]\\\') ???setting_one = driver.find_element_by_xpath(\\\'//*[@ and @name="tj_settingicon"]\\\') ???ActionChains(driver).drag_and_drop(setting, setting_one).perform() ???driver.find_element_by_xpath(\\\'//*[text()="搜索设置"]\\\').click() ???time.sleep(5) ???

drag_and_drop中要传两个参数,传入第一个是源元素setting后按住,再点击传入的目标元素setting_one后,然后再释放掉setting_one

drag_and_drop_by_offset
?

首先在源元素上单击并按住,移至给定的偏移量,然后释放鼠标。可针对验证码滑动解锁操作

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.common.exceptions import UnexpectedalertPresentExceptionwith webdriver.Chrome() as driver: ???driver.get(\\\'https://mail.qq.com/\\\') ???time.sleep(2) ???form_ = driver.find_element_by_xpath(\\\'//*[@]\\\') ???driver.switch_to.frame(form_) ???driver.find_element_by_xpath(\\\'//*[@]\\\').click() ???driver.find_element_by_xpath(\\\'//*[@]\\\').send_keys(\\\'16688888888\\\') ???driver.find_element_by_xpath(\\\'//*[@]\\\').click() ???driver.find_element_by_xpath(\\\'//*[@]\\\').send_keys(\\\'12345678999\\\') ???driver.find_element_by_xpath(\\\'//*[@]\\\').click() ???time.sleep(2) ???code_iframe = driver.find_element_by_xpath(\\\'//*[@]\\\') ???driver.switch_to.frame(code_iframe) ???code_offset = driver.find_element_by_xpath(\\\'//*[@]\\\') ???ActionChains(driver).drag_and_drop_by_offset(code_offset, 179, 0).perform() ???time.sleep(10)

官方文档中的介绍是可以这样操作:

from selenium import webdriverdriver = webdriver.Chrome()driver.get("https://crossbrowsertesting.github.io/drag-and-drop")sourceEle = driver.find_element(By.ID, "draggable")targetEle ?= driver.find_element(By.ID, "droppable")targetEleXOffset = targetEle.location.get("x")targetEleYOffset = targetEle.location.get("y")webdriver.ActionChains(driver).drag_and_drop_by_offset(sourceEle, targetEleXOffset, targetEleYOffset).perform() ?

但是实操使用了location.get()的方法,运行后报错了:

/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.pyTraceback (most recent call last): ?File "/Users/lifeng/python-projects/test-python/selenium_script.py", line 29, in <module> ???xOffset = code_offset.location(170)TypeError: \\\'dict\\\' object is not callableProcess finished with exit code 1 release
?

将释放按下的鼠标左键

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver: ???driver.get(\\\'https://www.runoob.com/python/python-tutorial.html\\\') ???text_python = driver.find_element_by_xpath(\\\'//*[@rel="noopener noreferrer" and text()="Python 3.X 版本的教程"]\\\') ???target_python = driver.find_element_by_xpath(\\\'//*[@rel="noopener noreferrer" and text()="Python 3.X 版本的教程"]\\\') ???webdriver.ActionChains(driver).click_and_hold(text_python).move_to_element(target_python).perform() ???webdriver.ActionChains(driver).release().perform() ???time.sleep(1)    

以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你

其它知识推荐

溜溜百科知识网——分享日常生活学习工作各类知识。 垃圾信息处理邮箱 tousu589@163.com
icp备案号 闽ICP备14012035号-2 互联网安全管理备案 不良信息举报平台 Copyright 2023 www.6za.net All Rights Reserved