【python+selenium的web自动化】- 元素的常用操作详解(二)
如果想从头学起selenium,可以去看看这个系列的文章哦!
https://www.cnblogs.com/miki-peng/category/1942527.html
本篇主要内容:1.鼠标操作;2.键盘操作;3.下拉框操作;4.js处理
上篇主要内容:1.元素的基本操作;2.等待操作;3.iframe操作;4.alert弹出框 传送门
鼠标操作
selenium的ActionChains类提供了一系列模拟鼠标操作的方法,主要操作流程:1、存储鼠标操作;2、调用perform()执行鼠标操作,支持的操作部分列举如下:
double_click(ele):双击context_click(ele):右键drag_and_drop(source, target):从某个元素拖拽到某个元素然后松开drag_and_drop_by_offset(source, xoffset, yoffset):拖拽元素到某个坐标然后松开move_to_element(ele):鼠标悬停在某个元素
下面以百度首页的搜索设置为例,鼠标悬停在【设置】,然后点击显示出来的【搜索设置】:
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# 启动Chromedriver,并与Chromedriver开启会话
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")
# 第一步:实例化ActionChains
ac = ActionChains(driver)
# 第二步:定位要操作的元素
ele = driver.find_element_by_xpath('//span[@id="s-usersetting-top"]')
# 第三步:执行对应的操作
ac.move_to_element(ele) # 悬浮操作
# 第四步,释放鼠标动作
ac.perform()
# 点击悬停出来的页面上的元素
wait = WebDriverWait(driver, 10)
loc = (By.XPATH, '//a[@class="setpref"]')
wait.until(EC.visibility_of_element_located(loc))
driver.find_element(*loc).click()
time.sleep(2)
driver.quit()
这里科普一个知识点,就是链式调用:它可以在一个方法后面接着调用另外的方法,只要它的方法都是返回对象本身即可,如上述的actionchains 用法可以这样写: ActionChains(driver).move_to_element(ele).perform()

用一个简单例子来演示一下ActionChains工作流程的原理:
def move_to():
print("正在移动")
def click():
print('点击')
actions = []
actions.append(move_to)
actions.append(click)
for action in actions: # perform
# action = move_to
action()
键盘操作
selenium提供了比较完整的键盘操作,在使用键盘按键方法前需要先导入keys类:from selenium.webdriver.common.keys import Keys,它定义了非常多的按键操作,具体的可以问度娘。
常见的一些组合键和非组合键如下:
send_keys(Keys.BACK_SPACE)删除键send_keys(Keys.SPACE)空格键-
【python+selenium的web自动化】- 元素的常用操作详解(二)的更多相关文章
- 【python+selenium的web自动化】- 元素的常用操作详解(一)
如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html 本篇主要内容:1.元素 ...
- 【Selenium01篇】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 二.话不多说,直接开干,开始搭建自动化测试环境 这里以前在 ...
- 【Selenium05篇】python+selenium实现Web自动化:读取ini配置文件,元素封装,代码封装,异常处理,兼容多浏览器执行
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第五篇博 ...
- 【Selenium03篇】python+selenium实现Web自动化:元素三类等待,多窗口切换,警告框处理,下拉框选择
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第三篇博 ...
- 【Selenium02篇】python+selenium实现Web自动化:鼠标操作和键盘操作!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第二篇博 ...
- 【Selenium06篇】python+selenium实现Web自动化:日志处理
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第六篇博 ...
- 【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第四篇博 ...
- 【Selenium07篇】python+selenium实现Web自动化:PO模型,PageObject模式!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第七篇博 ...
- 《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)
1.简介 在使用appium写app自动化的时候介绍toast的相关元素的定位,在Web UI测试过程中,也经常遇到一些toast,那么这个toast我们这边如何进行测试呢?今天宏哥就分两篇介绍一下. ...
随机推荐
- codefforces 877B
B. Nikita and stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...
- C# 数据类型(3)
动态类型 dynamic types 动态类型是后来引进的,他其实是一个static type,但是不像其他的静态类型,编译器不会检查你到底是啥类型(也不会检查你能不能去call某个'method') ...
- js uppercase first letter
js uppercase first letter const str = `abc`; str.slice(0, 1).toUpperCase(); // "A" str.sli ...
- webpack remove console.log
webpack remove console.log https://stackoverflow.com/questions/41040266/remove-console-logs-with-web ...
- LeetCode 高效刷题路径
LeetCode 高效刷题路径 Hot 100 https://leetcode.com/problemset/hot-100/ https://leetcode-cn.com/problemset/ ...
- mobile app & ppi & dpi & px
mobile app & ppi & dpi & px How do dp, dip, dpi, ppi, pixels and inches relate? https:// ...
- open an iOS simulator from the terminal
open an iOS simulator from the terminal # simulator $ open -a Simulator flutter https://flutter.dev/ ...
- macOS & Xnip
macOS & Xnip close finished notation cancel checked xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许 ...
- css & clip-path
css & clip-path https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path https://tongqu.me/ tw ...
- (1)MySQL进阶篇在linux环境下安装
1.概述 对于mysql二进制安装,优点是可以安装到任何路径下,灵活性好,一台服务器可以安装多个mysql.缺点是已经编译过,性能不如源码编译得好,不能灵活定制编译参数.如果用户即不想安装最简单却不够 ...
- 【python+selenium的web自动化】- 元素的常用操作详解(一)