鼠标表明鼠标事情。鼠标实际操作是根据应用低等界面实行的,该界面容许大家向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时要传2个主要参数,传到第一个是源元素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 <module>
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)
以上汇总也许能幫助到你,也许协助不了你,但還是期待能幫助到你
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。