


Python practical analysis of the basic elements of selenium and keyboard and mouse simulation events
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.
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()
The running results are as follows:
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:
##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"
# 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()
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"
PS: It will not happen in the actual scene It uses very complex mouse operation events to writekey_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
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()
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
