The best finishing, selenium automated testing - mouse/keyboard operation (details in actual combat)


foreword

selenium mouse operation

import HTML page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>小菠萝测试笔记</title>
</head>
<body>

<a>用户名:</a>
<input id="username" class="username">
<a>密码</a>
<input id="password" name="password">
<button class="login">登录</button>
<br>
<p>测试啦</p>
<p>再一次测试</p>
<br>
<a href="https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_1" target="_blank" id="virus-202s0"
   class="mnav sp dot">终极抗击肺炎啊</a>
<a href="https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_1" target="_blank" id="virus-2020"
   class="mnav sp dot">抗击肺炎</a>
<a href="https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_1" target="_blank" id="virus-202s0"
   class="mnav sp dot">抗击肺炎</a>

<div>
    <ul>
        <li class="li">111</li>
        <li class="li">222</li>
        <li class="li">333</li>
    </ul>
    <ul>
        <li>aaa</li>
        <li>bbb</li>
        <li>ccc</li>
    </ul>
</div>
</body>
</html>

Using selenium for automation, sometimes you will encounter situations that require simulated mouse operations, such as clicking, double-clicking, right-clicking, dragging, and so on.

And selenium provides us with a class to handle such events - ActionChains

What mouse actions are there?
Execute operation;
left click, right click, double click;
mouse hover to element, offset;
long press; drag;

1. The
perform() method of performing operations
is mainly to call this method again after calling other operation methods, indicating that a certain mouse operation is performed, and there will be examples later

Left click, right click, double click

from selenium.webdriver import ActionChains
from selenium import webdriver

driver = webdriver.Chrome("../resources/chromedriver.exe")

# 创建实例
chains = ActionChains(driver)

# 访问网址
driver.get("file:///C:/test.html")

# 登录按钮
username = driver.find_element_by_id("username")
login_btn = driver.find_element_by_class_name("login")
password = driver.find_element_by_id("password")

# 左键点击
chains.click(username).perform()

# 右键点击
chains.context_click(username).perform()

# 双击
chains.double_click(password).perform()

mouse over element, offset

# 悬停到设置按钮
chains.move_to_element(login_btn).perform()

# 悬停到指定偏移量
chains.move_to_element_with_offset(login_btn, 2, 2).perform()

Note:
move_to_element_with_offset() is to find the element first, and then specify the offset according to the element position offset

Press

# 长按
chains.click_and_hold(login_btn).perform()

drag

# 拖动1
chains.drag_and_drop(source=username, target=password)

# 拖动2
chains.drag_and_drop_by_offset(source=username, xoffset=20, yoffset=20)

Note:
drag_and_drop drags the source element to the target element
drag_and_drop_by_offset drags the source element to the specified offset

Selenium keyboard operation

What keyboard operation? Delete
key; space bar; tab key; back key
;
Wait, wait...
I won't give an example here, it's easy to understand by looking at the source code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep

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

driver = webdriver.Chrome("../resources/chromedriver.exe")
driver.maximize_window()

# 访问网址
driver.get("http://www.baidu.com")

# 找到搜索框
inputElement = driver.find_element_by_id("kw")

# 输入搜索内容
inputElement.send_keys("小菠萝测试笔记")

# ctrl+a全选
inputElement.send_keys(Keys.CONTROL, "a")

sleep(1)

# ctrl+c 复制输入框内容
inputElement.send_keys(Keys.CONTROL, 'c')

sleep(1)

# ctrl+x 剪切输入框内容
inputElement.send_keys(Keys.CONTROL, 'x')

sleep(1)

# ctrl+v 粘贴输入框内容
inputElement.send_keys(Keys.CONTROL, 'v')

sleep(1)

# 空格键
inputElement.send_keys(Keys.SPACE)

sleep(1)

# 后退键
inputElement.send_keys(Keys.BACKSPACE)

sleep(1)

# tab键
inputElement.send_keys(Keys.TAB)

sleep(1)

# 回车键
inputElement.send_keys(Keys.ENTER)

# 刷新页面
inputElement.send_keys(Keys.F5)

If you want to see what else to do, just ctrl+click Keys to view the source code

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Every effort is paving the way for future glory. Don't be afraid of difficulties and challenges, because your struggle will write endless possibilities. Believe in yourself and persevere, you will be able to create your own brilliant chapter!

Only by striving can one transcend the ordinary self; only by persisting can one create an extraordinary life; only by going forward bravely can one reap endless possibilities. Believe in yourself, work hard, the future glory will belong to you!

As long as you work hard enough, you can make your dreams come out of the ground, make your hopes bloom in your heart, and usher in the dawn of glory and success. Believe in yourself, don't give up, every step of struggle is closer to success!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/131536040