Automated testing-Selenium mouse and keyboard events

Selenium is a tool for testing web applications. Selenium tests run directly in the browser, just like real users are operating. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, etc. The main functions of this tool include: test compatibility with browsers-test your application to see if it works well on different browsers and operating systems Test system functions-create regression tests to verify software functions and user needs. Supports automatic recording of actions and automatic generation of test scripts in .Net, Java, Perl and other languages.

In the previous blog post, there are some common methods of talking about elements. Next, record the mouse and keyboard events of the element

Mouse events
In WebDriver, these methods of mouse operation are encapsulated in the ActionChains class. The ActionChains class provides common methods for mouse operations:

perform (): execute all actions stored by ActionChains
context_click (): right-click
double_click (): double-click
drag_and_drop (): drag
move_to_element (): hover

Keyboard events
The following introduces several commonly used keyboard operations:

Keys.BACK_SPACE: Delete key
Keys.SPACE: Space key Keys.TAB:
Tab key
Keys.ESCAPE: Back key
Keys.ENTER: Enter key
Keys.CONTROL, "a": key combination, Ctrl + A
Keys.CONTROL, "X": key combination, Ctrl + X
Keys.CONTROL, "v": key combination, Ctrl + V
Keys.CONTROL, "c": key combination, Ctrl + C
Keys. F1: F1 key
Keys. F12: F12 key

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys

driver = Chrome(“C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe”)
driver.get(“https://www.baidu.com/”)

Simulate keyboard input "python"

driver.find_element_by_id(“kw”).send_keys(“python.”)

Delete the last character

driver.find_element_by_id(“kw”).send_keys(Keys.BACK_SPACE)

Simulate the keyboard to enter a space

driver.find_element_by_id(“kw”).send_keys(Keys.SPACE)

Simulate keyboard input "tutorial"

driver.find_element_by_id(“kw”).send_keys(“教程”)

Select all input boxes

driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,“a”)

Cut

driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,“x”)

Paste

driver.find_element_by_id(“kw”).send_keys(Keys.CONTROL,“v”)

Carriage return

driver.find_element_by_id(“kw”).send_keys(Keys.ENTER)

Published 15 original articles · praised 7 · views 4015

Guess you like

Origin blog.csdn.net/weixin_43988159/article/details/95078110