Selenium Python教程第7章:Selenium编程其它功能

news2024/10/6 4:06:47

在这里插入图片描述

7. Selenium其它功能

7.1 Action Chains 动作链功能

WebDriver只能模拟针对特定元素的click, send_keys 操作,无法执行鼠标移动、悬浮、按键,选择菜单等操作,需要通过 Action Chains 类来操作
如下面操作,打开主菜单项后,再点击弹出的子菜单项

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

也可以将多个动作排队,最后再执行

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")
  
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

Selenium Python动作链方法
可以使用动作链执行大量操作,例如单击,右键单击等。以下是动作链中使用的重要方法的列表

MethodDescription
clickClicks an element.
click_and_holdHolds down the left mouse button on an element.
context_clickPerforms a context-click (right click) on an element.
double_clickDouble-clicks an element.
drag_and_dropHolds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
drag_and_drop_by_offsetHolds down the left mouse button on the source element, then moves to the target offset and releases the mouse button.
key_downSends a key press only, without releasing it.
key_upReleases a modifier key.
move_by_offsetMoving the mouse to an offset from current mouse position.
move_to_elementMoving the mouse to the middle of an element.
move_to_element_with_offsetMove the mouse by an offset of the specified element, Offsets are relative to the top-left corner of the element.
performPerforms all stored actions.
pausePause all inputs for the specified duration in seconds
releaseReleasing a held mouse button on an element.
reset_actionsClears actions that are already stored locally and on the remote end
send_keysSends keys to current focused element.

7.2 Alert 告警提示窗口相关操作

Selenium中的告警窗是一个小消息框,出现在屏幕上,为用户提供一些信息或通知。它通知用户一些特定信息或错误,请求执行某些任务的权限,还提供警告消息。

Alert 告警窗口的类型

1) 简单告警窗
在这里插入图片描述
主要用于显示一些信息

2)输入型提示窗
此类告警空会询问用户的一些输入,Selenium 网络驱动程序可以使用 sendkeys(“ input…").
在这里插入图片描述
3) 确认窗口
提供几个操作选项,供用户选择
在这里插入图片描述

编程步骤
1)每当触发警报时,网页上都会出现一个弹出窗口。控件仅保留在父网页中。因此,Selenium Webdriver 的第一个任务是将焦点从父页面切换到警报弹出窗口。可以使用以下代码片段完成此操作。

alert_obj = driver.switch_to.alert
  1. 控件移动到弹出窗口后,我们可以使用推荐的方法对其执行不同的操作。
    alert_obj.accept() – 用于接受提示窗口
    alert_obj.dismiss() – 用于取消提示窗口
    alert.send_keys() – 向提示窗口文本输入框输入内容
    alert.text() – 获取提示信息

处理简单告警窗口

简单告警窗上有一条消息和一个“确定”按钮。当它弹出时,用户单击“确定”按钮接受它。

这是HTML代码,它将在单击主页上的“创建告警”按钮时生成简单告警
Simple_Alert.html

<!DOCTYPE html>
<html>
<body bgcolor="#C0C0C0">
<h1>
Simple Alert Demonstration</h1>
<p>
click the Below Button to create an Alert</p>
<button onclick="alertFunction()" name ="alert"> Create Alert</button>
<script>
function alertFunction() {
 alert("Hi!, I am a Simple Alert. Please Click on the 'OK' Button.");
}
</script>
</body>
</html>

下面是selenium处理代码

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.maximize_window()
location = "file://<Specify Path to Simple_Alert.HTML>"
driver.get(location)

#Click on the "Alert" button to generate the Simple ert
button = driver.find_element_by_name('alert')
button.click()

#Switch the control to the Alert window
obj = driver.switch_to.alert

#Retrieve the message on the Alert window
msg=obj.text
print ("Alert shows following message: "+ msg )

time.sleep(2)

#use the accept() method to accept the alert
obj.accept()

print(" Clicked on the OK Button in the Alert Window")

driver.close

处理输入提示框

如下提示框:
在这里插入图片描述
selenium代码如下:


from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.maximize_window()
location = "file://<Specify Path to Prompt_Alert.HTML>"
driver.get(location)

#Click on the "employeeLogin" button to generate the Prompt Alert
button = driver.find_element_by_name('continue')
button.click()

#Switch the control to the Alert window
obj = driver.switch_to.alert

time.sleep(2)

#Enter text into the Alert using send_keys()
obj.send_keys('Meenakshi')

time.sleep(2)

#use the accept() method to accept the alert
obj.accept()

#Retrieve the message on the Alert window
message=obj.text
print ("Alert shows following message: "+ message )

time.sleep(2)

obj.accept()

#get the text returned when OK Button is clicked.
txt = driver.find_element_by_id('msg')
print(txt.text)

driver.close

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/645175.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

实战:用dockerfile创建镜像实现springboot项目容器化

文章目录 前言技术积累docker基本操作命令dockerfile简介dockerfile指令说明 实战演示创建dockerfile创建挂载目录构建dockerfile启动容器完成验证 写在最后 前言 docker容器化方案是当下流行的服务部署方式&#xff0c;在软件领域举足轻重。我公司的测试、线上环境都采用dock…

选择最适合你的云服务器:腾讯云、华为云、阿里云对比

云服务器是一种基于云计算技术的服务器&#xff0c;它可以为企业提供高效、灵活、安全的运行环境。目前市场上有很多云服务器的选择&#xff0c;其中腾讯云、华为云和阿里云是最受欢迎的三个品牌&#xff0c;下面我们来看看它们各自的优势。 腾讯云的优势在于其强大的技术支持…

谷粒商城p45-自动装配-stream流-lambda表达式

软件&#xff1a;idea、postman、virtual box 服务&#xff1a;gulimall-product 请求路径&#xff1a;http://localhost:10000/product/category/list/tree 启动&#xff1a;启动idea product模块&#xff0c;启动vm&#xff0c;启动docker mysql controller代码 自动装配C…

LeetCode494. 目标和 0-1背包DP

https://leetcode.cn/problems/target-sum/ 题目描述 给你一个整数数组 nums 和一个整数 target 。 向数组中的每个整数前添加 ‘’ 或 ‘-’ &#xff0c;然后串联起所有整数&#xff0c;可以构造一个 表达式 &#xff1a; 例如&#xff0c;nums [2, 1] &#xff0c;可以在…

网络传输中的那些编码之-UTF8编码漫谈

编码为什么是一个重要的话题&#xff0c;因为我们和计算机交互的主要方式目前还是文字字符。作为一个程序员&#xff0c;相信大部分都都被字符和编码的问题折磨过&#xff0c;从键盘输入文字字符到编辑器 中&#xff0c;编辑器存储字符到硬盘&#xff0c;再到具体一个编程语言如…

中国电子学会2023年05月份青少年软件编程Scratch图形化等级考试试卷二级真题(含答案)

2023-05 Scratch二级真题 题数&#xff1a;37 分数&#xff1a;100 测试时长&#xff1a;60min 一、单选题(共25题&#xff0c;共50分) 1.运行下列哪段程序&#xff0c;可以让狗狗走到木屋门口&#xff1f;&#xff08;C&#xff09;(2分) A. B. C. D. 答案解析&a…

我用AI提高我的代码质量,周边同事对我的代码赞不绝口,速来围观

文章目录 前言功能演示1.使用Stream API来简化集合操作2.使用switch语句来替代多个if-else语句3.使用try-with-resources语句来自动关闭资源4. Lambda 表达式来简化代码,并提高代码的可读性和可维护性5.查找代码中的bug并优化6.python 使用sort方法来对列表进行排序7.javaScrpi…

一文看懂MySQL是什么

你可以前往我的博客查看更多关于云服务器和数据库以及域名注册、建站等相关文章。 MySQL是一种开源关系型数据库管理系统&#xff0c;它是最受欢迎的数据库之一。MySQL是由瑞典公司MySQL AB创建的&#xff0c;后来被Sun Microsystems收购&#xff0c;现在是Oracle Corporation…

Flink 系列二 Flink 状态化流处理概述

本篇作为Flink系列的第二篇&#xff0c;第一篇是环境准备&#xff0c;需要的同学可以看&#xff1a;https://blog.csdn.net/lly576403061/article/details/130358449?spm1001.2014.3001.5501。希望可以通过系统的学习巩固该方面的知识&#xff0c;丰富自己的技能树。废话不多说…

解决 org.eclipse.jface.text.Document class file version 61.0 报错

问题描述 运行好好的项目&#xff0c;没有做任何改动&#xff0c;最近在编译时报以下错误 java.lang.UnsupportedClassVersionError: org/eclipse/jface/text/Document has been compiled by a more recent version of the Java Runtime (class file version 61.0), this vers…

解决Python使用pip安装库文件出现“ERROR: Cannot unpack file…”

解决问题 1 ERROR: Could not find a version that satisfies the requirement robotframework (from versions: none) ERROR: No matching distribution found for robotframework 在dos命令输入 pip install robotframework 在线安装robotframework 如下报错&#xff1a; …

在紧急情况下,120可以定位我们的位置吗

随着科技的快速发展&#xff0c;越来越多的人们开始意识到科技对于生活的重要性。在现代社会中&#xff0c;GPS定位系统已经成为了一个不可或缺的工具&#xff0c;并且被广泛应用于各个领域&#xff0c;包括医疗救援行业。 120急救车和120急救指挥调度系统都采用了GPS定位技术…

SpringCloud Eureka注册中心高可用集群配置(八)

当注册中心扛不住高并发的时候&#xff0c;这时候 要用集群来扛&#xff1b; 我们再新建两个module microservice-eureka-server-2002 microservice-eureka-server-2003 第一步&#xff1a; pom.xml 把依赖加下&#xff1a; <dependencies> <dependency…

失败的统一错误处理

1.拦截器 在调用接口的时候,客户端会向服务器发送请求,请求之前有请求拦截器&#xff0c;返回数据之前有响应拦截器。 2:示例 根据自己的状态码来进行判断的一般2字开头代表成功&#xff0c;这个状态码是由后端来进行控制的。 成年的代码处理: if (res.data.success) {// 成功…

都是被逼的... ,LM算法的具体实现python和C++代码

L-M方法全称Levenberg-Marquardt方法&#xff0c;是一种非线性最小二乘优化算法&#xff0c;它通过同时利用高斯牛顿方法和梯度下降方法来解决非线性最小二乘问题。其核心思想是在每次迭代中&#xff0c;根据当前参数估计计算目标函数的梯度和海森矩阵&#xff0c;并使用这些信…

华为OD机试之最大N个数与最小N个数的和

最大N个数与最小N个数的和 题目描述 给定一个数组&#xff0c;编写一个函数来计算它的最大N个数与最小N个数的和。你需要对数组进行去重。 说明&#xff1a; 数组中数字范围[0, 1000]最大N个数与最小N个数不能有重叠&#xff0c;如有重叠&#xff0c;输入非法返回-1输入非法返…

Python之pyecharts的常见用法3-极坐标图-漏斗图

Pyecharts是一个基于Echarts的Python可视化库&#xff0c;可以用Python语言轻松地生成各种交互式图表和地图。它支持多种图表类型&#xff0c;包括折线图、柱状图、散点图、饼图、地图等&#xff0c;并且可以通过简单的API调用实现数据可视化。 Pyecharts的优点包括&#xff1a…

Python编程入门基础及高级技能、Web开发、数据分析和机器学习与人工智能

文章目录 入门基础安装 Python 环境&#xff0c;选择一个 IDE&#xff0c;如 PyCharm、VSCode等。学习基本语法&#xff1a;变量、数据类型、条件语句、循环语句、函数、异常处理等。熟悉标准库&#xff1a;常用模块、内置函数等。学习基本的面向对象编程&#xff08;OOP&#…

Doris数仓的4大特点

01-极简架构 Doris从设计上来说&#xff0c;融合了Google Mesa的数据存储模型、Apache的ORCFile存储格式、Apache Impala查询引擎和MySQL交互协议&#xff0c;是一个拥有先进技术和先进架构的领先设计产品&#xff0c;如图1所示。 ▲图1 Doris技术分解图 在架构方面&#xff…

Android Binder机制浅谈以及使用Binder进行跨进程通信的俩种方式(AIDL以及直接利用Binder的transact方法实现)

Binder机制学习 Binder机制是Android进行IPC&#xff08;进程间通信&#xff09;的主要方式Binder跨进程通信机制&#xff1a;基于C/S架构&#xff0c;由Client、Server、ServerManager和Binder驱动组成。 进程空间分为用户空间和内核空间。用户空间不可以进行数据交互&#xf…