連點器速度測試「鼠標連點測試」

鼠標表示鼠標事件。鼠標操作是通過使用低級界面執行的,該界面允許我們向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 <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)
    

以上總結或許能幫助到你,或許幫助不到你,但還是希望能幫助到你

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/205662.html

(1)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-07 17:48
下一篇 2024-12-07 17:48

相關推薦

發表回復

登錄後才能評論