鼠标连点测试(鼠标测试网页)

启航的学习笔记 4 0

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

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

click_and_hold
import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver:    driver.get('https:///')    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 timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver:    driver.get('https:///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:///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:///')    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
import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainswith webdriver.Chrome() as driver:    driver.get('https:///')    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.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:///')    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 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('//*[@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 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 ActionChainswithwebdriver.Chrome()asdriver:    driver.get('https:///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)

以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,评论区留言会及时修正发布,谢谢

未完,待续…一直都在努力,希望您也是!

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