Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events

WBOY
Release: 2022-06-14 19:11:26
forward
2768 people have browsed it

This article brings you relevant knowledge about python, which mainly introduces the basic elements of selenium and issues related to keyboard and mouse simulation events, including the use of Keys module to simulate keyboard operation events. , use the Action class to simulate mouse operation events, etc. Let’s take a look at it together. I hope it will be helpful to everyone.

Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events

Recommended learning: python video tutorial

When we locate a specific element, we can specify the element operations, such as the click operation performed in the previous chapter. This is the simplest operation, webdriver There are other operations. For example, basic operations of elements (click, input, clear), as well as some advanced operations such as mouse and keyboard simulation events, pop-up box processing, multi-page switching, etc... These are all things that we need to understand, and they are often used when doing automated testing. Some basic scenarios encountered. In today's chapter, let's first learn the basic operations of elements and the operation of mouse and keyboard simulation events.

Basic operations of elements

Use the local form.html file we used before to practice the basic click, input, and clear operations of elements.

The code example is as follows:

# coding:utf-8

from time import sleep
from selenium import webdriver


driver = webdriver.Chrome()     # 启动 Chrome浏览器的 driver
driver.maximize_window()        # Chrome 浏览器最大化
driver.get('file:///Users/workspace/WEB_TEST_HTML/form.html')       # 打开本地的 "form.html" 文件
sleep(1)
email_element = driver.find_element_by_xpath('//*[@id="inputEmail"]')    # 通过 xpath 定位 Email 输入框。
email_element.send_keys('username')     # Email 输入框输入 "username"
sleep(1)
email_element.clear()                   # 清除 Email 输入框内容
sleep(1)
email_element.send_keys('admin')        # Email 输入框输入 "admin"

driver.find_element_by_xpath('//*[@id="inputPassword"]').send_keys('123456')    # Password 输入框输入 "123456"
sleep(1)
driver.find_element_by_xpath('/html/body/form/div[3]/div/button').click()		# 通过 xpath 定位 "Sign in" 按钮并点击

driver.quit()
Copy after login

The running results are as follows:


Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events


The above are elements Basic operations are actually the simplest and most basic operations. Next, let's continue to look at more difficult operations ---> Mouse and keyboard simulation event operations.

Mouse and keyboard simulation event operation

Use our local sendkeys.html file to implement mouse and keyboard simulation event operation. sendkeys.html The page elements of the file are as follows:


Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events


##Use the Keys module to simulate keyboard operation events

ps: Using the Keys module requires an import operation: "from selenium.webdriver.common.keys import Keys"

Keyboard simulation event code examples are as follows:

# coding:utf-8

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()     # 启动 Chrome浏览器的 driver
driver.maximize_window()        # Chrome 浏览器最大化
driver.get('file:///Users/workspace/WEB_TEST_HTML/sendkeys.html')   # 打开本地的 "form.html" 文件
sleep(1)

# 这里需要注意一下,因为我使用的是 Mac ,所以键盘 ctrl 事件是 "Keys.COMMAND" ,如果是 Win 系统的话,ctrl 事件是 "Keys.CONTROL"
driver.find_element_by_id('A').send_keys((Keys.COMMAND, 'a'))       # 通过 id 定位 "id = A" 的元素,执行键盘事件 command + a
driver.find_element_by_id('A').send_keys((Keys.COMMAND, 'x'))       # 通过 id 定位 "id = A" 的元素,执行键盘事件 command + x
sleep(1)
driver.find_element_by_id('B').send_keys((Keys.COMMAND, 'v'))       # 通过 id 定位 "id = B" 的元素,执行键盘事件 command + v
sleep(1)
driver.find_element_by_id('B').send_keys((Keys.COMMAND, 'a'))       # 通过 id 定位 "id = B" 的元素,执行键盘事件 command + a
sleep(1)
driver.find_element_by_id('B').send_keys((Keys.COMMAND, 'c'))       # 通过 id 定位 "id = B" 的元素,执行键盘事件 command + c
sleep(1)
driver.find_element_by_id('A').send_keys((Keys.COMMAND, 'v'))       # 通过 id 定位 "id = A" 的元素,执行键盘事件 command + v
sleep(1)

driver.quit()
Copy after login
Run results As follows:


Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events


Use the Action class to simulate mouse operation events

PS: There are not many scenarios for simulating mouse operations. Just understand. At the same time, the Action class needs to execute "from selenium.webdriver import ActionChains"

Let's first take a look at what common mouse operations the Action class supports.

    key_down: Simulate a mouse button press
  • key_up: Simulate a mouse button pop-up
  • click: Simulate a mouse button click (click)
  • context_click: Click the right mouse button
  • double_click: Simulate a mouse button click (double-click)
  • send_keys: Send a key to the currently focused element
  • click_and_hold: Click Left mouse button, do not release (drag)
  • release: release, release the pressed mouse button
  • move_to: move the mouse to...
  • drag_and_drop: drag and drop Get up and throw it away...
  • perform: No matter what operation is done, you need to
  • perform in the end to submit
PS: It will not happen in the actual scene It uses very complex mouse operation events to write

automated Case, so what we demonstrate is also a relatively simple scenario.

Simulating mouse events The code example is as follows:

# coding:utf-8

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains


driver = webdriver.Chrome()     # 启动 Chrome浏览器的 driver
driver.maximize_window()        # Chrome 浏览器最大化
driver.get('file:///Users/workspace/WEB_TEST_HTML/sendkeys.html')   # 打开本地的 "form.html" 文件
sleep(1)

# 这里需要注意一下,因为我使用的是 Mac ,所以键盘 ctrl 事件是 "Keys.COMMAND" ,如果是 Win 系统的话,ctrl 事件是 "Keys.CONTROL"
double_click_element = driver.find_element_by_id('A')
# 通过 id 定位 "id = A" 的元素赋值给 double_click_element

ActionChains(driver).double_click(double_click_element).context_click(double_click_element).perform()
# 通过 ActionChains 类将 "driver" 转换,先双击、然后执行右击操作【这种串联起来的操作,叫做链式用法,可以根据这个链一直往下写】
sleep(2)

ActionChains(driver).context_click(double_click_element).perform()
# 通过 ActionChains 类将 "driver" 转换,然后执行右击操作
sleep(2)

driver.quit()
Copy after login
The running results are as follows:


Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events

The above is Use the

Action class to implement the simulation of some special scenes. The more commonly used ones are double-click, right-click, drag and other scenes are used slightly more, and other scenes use Action There will be very few categories.

Recommended learning:

python video tutorial

The above is the detailed content of Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template