阅读 271

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

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

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

click_and_hold

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

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com/')
    time.sleep(2)
    setting = driver.find_element_by_xpath('//*[@id="s-usersetting-top" and text()="设置"]')
    ActionChains(driver).click_and_hold(setting).perform()
    time.sleep(5)

context_click

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


import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with 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 time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with 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 time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com/')
    setting = driver.find_element_by_xpath('//*[@id="s-usersetting-top" 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 time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com/')
    setting = driver.find_element_by_xpath('//*[@id="s-usersetting-top" 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.py
Traceback (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 time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com/')
    setting = driver.find_element_by_xpath('//*[@id="s-usersetting-top" and @name="tj_settingicon"]')
    setting_one = driver.find_element_by_xpath('//*[@id="s-usersetting-top" 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 time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException


with webdriver.Chrome() as driver:
    driver.get('https://mail.qq.com/')
    time.sleep(2)
    form_ = driver.find_element_by_xpath('//*[@id="login_frame"]')
    driver.switch_to.frame(form_)
    driver.find_element_by_xpath('//*[@id="u"]').click()
    driver.find_element_by_xpath('//*[@id="u"]').send_keys('16688888888')
    driver.find_element_by_xpath('//*[@id="p"]').click()
    driver.find_element_by_xpath('//*[@id="p"]').send_keys('12345678999')
    driver.find_element_by_xpath('//*[@id="login_button"]').click()
    time.sleep(2)
    code_iframe = driver.find_element_by_xpath('//*[@id="tcaptcha_iframe"]')
    driver.switch_to.frame(code_iframe)
    code_offset = driver.find_element_by_xpath('//*[@id="tcaptcha_drag_button"]')
    ActionChains(driver).drag_and_drop_by_offset(code_offset, 179, 0).perform()
    time.sleep(10)

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


from selenium import webdriver


driver = 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.py
Traceback (most recent call last):
  File "/Users/lifeng/python-projects/test-python/selenium_script.py", line 29, in 
    xOffset = code_offset.location(170)
TypeError: 'dict' object is not callable

Process finished with exit code 1

release

将释放按下的鼠标左键


import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


with 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)
    

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

文章分类
百科问答
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐