Web automated testing (basic learning part 2)

mouse, keyboard events

1. Mouse

1. 1ActionChains

        ActionChains are a low-level method for automating interactions such as mouse movement, mouse button actions, key presses, and context menu interactions. This is useful for performing more complex actions such as hover and drag and drop.
        ActionChains can be used in chains:
        menu = driver.find_element_by_css_selector(".nav")
        hidden_submenu = driver.find_element_by_css_selector(".nav#submenu1")
        ActionChains(driver).move_to_element(menu).click(hidden_submenu).per form ()
        or actions can be queued one by one and then executed:
        menu = driver.find_element_by_css_selector(".nav")
        hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
        actions = ActionChains(driver)
        actions.move_to_element(menu)
        actions.cl ick (hidden_submenu)
        actions. perform()

1.2 Basic Usage

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains  # 引入 ActionChains 类
 
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
 
# 定位到要右击的元素
right_click = driver.find_element_by_id("xxxx")
 
# 对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(right_click).perform()#链式写法
# 分步写法
actions = ActionChains(driver)#调用 ActionChains()类,将浏览器驱动driver作为参数传入。

actions.click()# 装载单击动作

actions.context_click()# 装载右击动作,用于模拟鼠标右击操作,在调用时需要指定元素定位。

actions.perform()# 执行所有被装载的动作,可以理解为是对整个操作的提交动作。

1.3 API Summary

  • perform(self): ---Execute all actions in the chain
  • reset_actions(self): ---Clear the actions stored in the remote
  • click(self, on_element=None): ---click the left mouse button
  • click_and_hold(self, on_element=None): --Click the left mouse button without releasing it
  • context_click(self, on_element=None): --- mouse right click
  • double_click(self, on_element=None): --- Double click the left mouse button
  • drag_and_drop(self, source, target): ---Drag to an element and release it, where source: the source element dragged by the mouse, target: the target element released by the mouse.
  • drag_and_drop_by_offset(self, source, xoffset, yoffset): ---Drag to a certain coordinate and release
  • key_down(self, value, element=None): ---A keyboard key is pressed
  • key_up(self, value, element=None): --- Release a key
  • move_by_offset(self, xoffset, yoffset): ---Mouse moves to a certain coordinate
  • move_to_element(self, to_element): ---Mouse moves to an element
  • move_to_element_with_offset(self, to_element, xoffset, yoffset): ---Move to the position from an element (upper left corner)
  • release(self, on_element=None): --- Release the mouse on an element
  • send_keys(self, *keys_to_send): ---Send certain values ​​to the currently focused element
  • send_keys_to_element(self, element, *keys_to_send): --- Send some value to the specified element

2. Keyboard

from selenium.webdriver.common.keys import Keys#引入包

2.1 Accessory keys

  • key_down #a keyboard key is pressed

  • key_up # release a key

  • send_keys #Send some value to the current focus element

  • send_keys_to_element #Send some value to the specified element

2.2keys

#输入框输入内容
driver.find_element_by_id("kw").send_keys("sanmu1")
#删除多输入的一个1
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
#输入空格键+“szj”
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys(u"szj")
#ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
#ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
#ctrl+v 粘贴内容到输入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')
#通过回车键盘来代替点击操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
#回退键 ESC
driver.find_element_by_id("su").send_keys(Keys.ESCAPE)
#制表键(Tab)
driver.find_element_by_id("su").send_keys(Keys.TAB) 
#键盘 F1
driver.find_element_by_id("su").send_keys(Keys.F1) 

Guess you like

Origin blog.csdn.net/m0_58807719/article/details/130044281