当前位置:网站首页>Learn selenium to simulate mouse operation, and you can be lazy a little bit

Learn selenium to simulate mouse operation, and you can be lazy a little bit

2022-07-01 16:03:00 TEST_ Two black

Preface

We're doing it Web When automating , Sometimes the elements of the page don't need us to click , Value needs to move the mouse to display all kinds of information .

At this time, we can operate the mouse to achieve , Next, let's talk about using selenium do Web How to operate the mouse during Automation .

The mouse operation , We can use selenium Of ActionChains Class to achieve , Let's get to know this class first .

01.ActionChains Class common methods

  • click_and_hold(on_element=None) : Left click , Do not release

  • context_click(on_element=None): Right click

  • double_click(on_element=None): Double click the left mouse button

  • drag_and_drop(source, target): Drag to an element and release

  • move_by_offset(xoffset, yoffset): Move the mouse from the current position to a certain coordinate

  • move_to_element(to_element) : Move the mouse to an element

  • release(on_element=None): Release the pressed mouse button on the element

  • pause(seconds): Suspend operation ( second )

02.ActionChains Class all methods

  • perform(self)– Execute mouse operation method

  • reset_actions()– Clear operating instructions

  • click(on_element=None)– Left click

  • click_and_hold(on_element=None): Left click , Do not release

  • context_click(on_element=None): Right click

  • double_click(on_element=None): Double click the left mouse button

  • drag_and_drop(source, target): Drag to an element and release

  • drag_and_drop_by_offset(source, xoffset, yoffset) : Drag to a coordinate and release

  • key_down(value, element=None): Press... On a keyboard

  • key_up(value, element=None) : Loosen a

  • move_by_offset(xoffset, yoffset): Move the mouse from the current position to a certain coordinate

  • move_to_element(to_element) : Move the mouse to an element

  • move_to_element_with_offset(to_element, xoffset, yoffset): Move to

  • From an element ( Top left coordinates ) How far away is the location

  • pause(seconds): Suspend operation ( second )

  • release(on_element=None): Release the pressed mouse button on the element

  • send_keys(*keys_to_send): Send a key to the current focus element

  • send_keys_to_element(element, *keys_to_send) : Send a key to the specified element

03.ActionChains Use steps

  • Instantiation :actions = ActionChains(driver)
  • Call the mouse operation method :actions.move_to_element(menu)
  • Execute mouse operation method :actions.perform()

04. actual combat

What the code does :

  • Open the test web page :https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover
  • Switch iframe
  • Navigate to the bound mouse event div
  • Move the mouse to div Event triggered on the ( Can be observed div The words on the will change )
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(r"D:\chromeDriver\71\chromedriver71.exe")
driver.implicitly_wait(5)
driver.get(url="https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover")
driver.maximize_window()
# Switch iframe
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="iframeResult"]'))
# Element localization 
ele = driver.find_element_by_xpath('//div[@οnmοuseοver="mOver(this)"]')
# Move the mouse over the element to trigger the event 
actions = ActionChains(driver)
actions.move_to_element(ele)
actions.perform()
time.sleep(5)
driver.quit()

05. summary

ActionChains Other methods in the class are used in the same way , If you have time, you can try

ActionChains There are many methods in the class , But not many are commonly used , Listed above , Just master the common methods

Particular attention :ActionChains Implementation principle of , When you call ActionChains Method time , Not immediately , Instead, all operations are put in a queue in order , When you call perform() When the method is used , The time in the queue will execute in turn .

ActionChains Methods in a class can be called using a chain , Let's think about our own expansion ( It's very simple ).

Finally, thank everyone who reads my article carefully , The following online link is also a very comprehensive one that I spent a few days sorting out , I hope it can also help you in need ! Insert picture description here
These materials , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful …….

原网站

版权声明
本文为[TEST_ Two black]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011550196685.html