首页 > 其他分享 >Selenium鼠标事件

Selenium鼠标事件

时间:2023-03-28 17:23:28浏览次数:32  
标签:鼠标 ActionChains Selenium 元素 driver element 事件 id

前言:执行自动化测试过程中遇到鼠标的操作,例如:左键单击、左键双击、右键单击、鼠标悬停、鼠标拖动等等操作,如何模拟鼠标的操作?

1、导入ActionChains包

想使用selenium中的鼠标事件,首先我们必须导入ActionChains包,需要注意的是包名称ActionChains两个单词首字母需要大写

from selenium.webdriver.common.action_chains import ActionChains  # 使用鼠标操作,要导入ActionChains包

2、鼠标操作分类

1)鼠标事件 proform()
调用 ActionChains 类方法时,不会立即执行,而是将所有操作都存储在一个队列里,当调用 perform() 方法时,队列里的操作会依次执行。可以理解为对鼠标事件的提交操作,所以首先要记住这个方法。

2)鼠标事件分类

  • perform():执行所有存储的动作

  • click(on_element=None):单击鼠标左键
    on_element:指被点击的元素,如果该参数为None,将单击当前鼠标所在位置

  • double_click(on_element=None):双击鼠标左键
    on_element:指被点击的元素,如果该参数为None,将单击当前鼠标所在位置

  • click_and_hold(on_element=None):长按鼠标左键
    on_element:指被点击的元素,如果该参数为None,将单击当前鼠标所在位置

  • context_click(on_element=None):单击鼠标右键
    on_element:指被点击的元素,如果该参数为None,将单击当前鼠标所在位置

  • move_to_element(to_element):鼠标悬停到某个元素
    to_element:鼠标悬停到该元素上

  • drag_and_drop(source, target):把元素拖动到另一个元素后松开
    Source:鼠标拖动的元素(元素定位)
    Target:鼠标释放的目标元素(元素定位)

  • drag_and_drop_by_offset(source, xoffset, yoffset):把元素拖到某个坐标然后松开
    Source:鼠标拖动的元素(元素定位)
    xoffset:x轴坐标
    yoffset:y轴坐标

  • move_by_offset(xoffset, yoffset):鼠标从当前位置移动到某个坐标

  • move_to_element_with_offset(to_element, xoffset, yoffset):鼠标移动到距某个元素(左上角坐标)多少距离的位置
    to_element:指鼠标移动后停止到该元素上
    xoffset:x轴坐标
    yoffset:y轴坐标

  • release(on_element=None):在某个元素位置松开鼠标左键
    on_element:被鼠标释放的元素

  • send_keys(*keys_to_send):发送单个或多个参数到当前焦点的元素
    *keys_to_send:可变参数,可同时传入多个参数,如send_keys("a", "b")

  • send_keys_to_element(element, *keys_to_send):发送单个或多个参数到指定元素
    element:定位的元素
    *keys_to_send:可变参数,可同时传入多个参数

  • key_down(value, element=None):按下某个键盘上的键
    value:输入的参数(键盘上的键)
    element:指被定位的元素,如果该参数为None,将当前定位元素输入

  • key_up(value, element=None):松开某个键盘上的键

3、鼠标操作方法

from selenium.webdriver.common.action_chains import ActionChains  # 使用鼠标操作,要导入ActionChains包

# 打开Chrome浏览器
driver = webdriver.Chrome("../login/chromedriver.exe")
# 打开本地的html代码
driver.get("file:///D:/test.html")

# 通过id定位到单击
clk = driver.find_element_by_id("d")
# 使用AActionChains的click()方法对指定元素点击鼠标左键
dan = ActionChains(driver).click(clk)
# 使用perform()执行
dan.perform()

# 通过id定位到双击
dou = driver.find_element_by_id("s")
# 使用ActionChains的double_click()方法对指定元素双击鼠标左键
sh = ActionChains(driver).double_click(dou)
# 使用perform()执行
sh.perform()

# 通过id定位到悬停
xt = driver.find_element_by_id("x")
# 使用ActionChains的move_to_element()方法使鼠标移动到指定元素悬停
xua = ActionChains(driver).move_to_element(xt)
# 使用perform()执行
xua.perform()

# 通过id定位到按下
ax = driver.find_element_by_id("a")
# 使用ActionChains的click_and_hold()方法长按左键
an = ActionChains(driver).click_and_hold(ax)
# 使用perform()执行
an.perform()

# 通过id定位到移动
tu = driver.find_element_by_id("ele")
# 使用ActionChains的drag_and_drop_by_offset()方法把元素移动到x坐标75,y坐标10
tuo = ActionChains(driver).drag_and_drop_by_offset(tu, 75, 10)
# 使用perform()执行
tuo.perform()

# 鼠标移动到x轴为80,y轴为80的位置
ActionChains(driver).move_by_offset(80, 80).perform()

# 释放被长按在按下的的鼠标
shi = ActionChains(driver).release(ax).perform()


time.sleep(1)
# 链式用法,把所有的执行的鼠标操作串联一起执行
ActionChains(driver).click(clk).double_click(dou).move_to_element(xt).click_and_hold(ax).drag_and_drop_by_offset(tu, 75, 10).perform()

# 点击id为aa的输入框并点击到输入框
driver.find_element_by_id("aa").click()
# 使用ActionChains的send_keys()方法在当前定位的元素输入值"123"和"456"
ActionChains(driver).send_keys("123", "456").perform()

# 定位id为bb的输入框
bb = driver.find_element_by_id("cc")
# 使用ActionChains的send_keys_to_element()方法传入定位元素,输入参数"abc"和"def"
ActionChains(driver).send_keys_to_element(bb, "abc", "def").perform()

# 浏览器打开新的地址,把元素拖动到另一个元素
driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")

# 切入到iframe框中
driver.switch_to.frame("iframeResult")
# 根据元素id获取要拖动元素
e = driver.find_element_by_id("draggable")
# 根据元素id获取拖动到的元素
x = driver.find_element_by_id("droppable")

# 使用ActionChains的drag_and_drop()方法把元素e拖动到元素e的位置
ActionChains(driver).drag_and_drop(e, x).perform()

# 获取弹框
alert = driver.switch_to.alert
# 使用弹框的accept()方法确定弹框
alert.accept()

4、HTML代码

<head>
		<meta charset="utf-8">
		<style>
			#container{
				width: 570px;
				height: 200px;
			}
			dl{
				float: left;
			}
			dt{
				width: 90px;
				height: 90px;
				background-color: lightblue;
				border-radius: 20px;
				margin: 10px;
			}

			.yellow{
				background-color: lightgoldenrodyellow;
			}
			.blue{
				background-color: lightblue;
			}
		</style>
		<title></title>
	</head>
	<body>
		<div id="container">
			<dl>
				<dt onclick="f1(this)" id='d'></dt>
				<dd>单击</dd>
			</dl>
			<dl>
				<dt ondblclick="f2(this)" id='s'></dt>
				<dd>双击</dd>
			</dl>
			<dl>
				<dt onm ouseover="this.className='yellow'" onm ouseout="this.className='blue'" id='x'></dt>
				<dd>悬停</dd>
			</dl>
			<dl>
				<dt onm ousedown="this.className='yellow'" onm ouseup="this.className='blue'" id='a'></dt>
				<dd>按下</dd>
			</dl>
			<dl>
				<dt id="ele" onm ousemove="mouseMove(event)"></dt>
				<dd>移动</dd>
			</dl>
			<br><span id="echo"></span>
		</div></br>
		</br>
		<div >
			<div>
				账号:<input id='aa'></br>
				密码:<input id='cc'>
			</div>
		<script>
			var num1=0,num2=0;
			function f1(e){
				num1++;
				if(num1%2==0)
					e.className="blue";
				else
					e.className="yellow";
			}
			function f2(e){
				num2++;
				if(num2%2==0)
					e.className="blue";
				else
					e.className="yellow";
			}
			function mouseMove(ev){				
				ev=ev||window.event;
				var el=document.getElementById("ele");
				var x=ev.clientX-el.offsetLeft;
				var y=ev.clientY-el.offsetTop;
				if(x<el.offsetWidth/2)
					el.style.background="lightgoldenrodyellow";
				else 
					el.style.background="lightpink";
				document.getElementById("echo").innerHTML="x:"+x+"<br>y:"+y;
			}
		</script>
	</body>

标签:鼠标,ActionChains,Selenium,元素,driver,element,事件,id
From: https://www.cnblogs.com/lc-blogs/p/17262358.html

相关文章

  • echarts点击事件,点击的是节点还是文字?
    通过params.event.target.culling的true和false判断,true:点击了节点,false:点击了文字myChart.on(‘click’,function(params){if(params.event.target.culling===tr......
  • 数据分析-家用热水器用户行为分析与事件识别
    显示数据importpandasaspdimportnumpyasnpdata=pd.read_excel('D:/人工智能&软件工程/数据挖掘与分析/data/original_data.xls')data[u'发生时间']=pd.to_......
  • Android开发 1_触控事件分发_触控在framework层上的注册
    前言此篇博客基于Android10版本,将讲解触控在系统层上的注册。Android的触控事件分发与其他事件分发来对比的话,可以说是很不一样的。如果你阅读此篇博客想去了解在fram......
  • JavaScript 触发click事件 兼容FireFox,IE 和 Chrome
    解决了火狐下无法触发click事件的问题 <scriptlanguage="javascript"> functiontest2(name) { if(document.all) { document.getElementById(name).click(); ......
  • selenium的作用域及解决登录验证问题
    一、selenium的作用域切换selenium在处理元素时遇见新窗口、网页嵌套网页、网页的原生弹窗,无法进行直接处理作用域里元素的内容,需要通过切换作用域来处理此类问题。selen......
  • 连续洗浴事件识别
    importpandasaspdimportmatplotlib.pyplotaspltinputfile='E:\Python/original_data.xls'#输入的数据文件data=pd.read_excel(inputfile)#读取数据......
  • Android TextView部分文字(超链接/Span)点击事件、变色、去除下划线
    马上8月结束了,深海决定写点东西分享给大家,祝各位程序猿身体健康万事如意.废话不多说,直接上图:如图中蓝色文字的效果,需求如下:1,点击跳转到另一个页面2.去除下划线3.颜色......
  • 连续洗浴事件
    #10-1importpandasaspdimportmatplotlib.pyplotaspltinputfile='original_data.xls'data=pd.read_excel(inputfile)lv_non=pd.value_counts(data['......
  • 连续洗浴事件识别
    importpandasaspdimportmatplotlib.pyplotaspltinputfile="C:\\Users\\Lenovo\\Desktop\\original_data.xls"#输入的数据文件data=pd.read_excel(inputf......
  • 连续洗浴事件识别
    importpandasaspdimportmatplotlib.pyplotaspltinputfile='D://人工智能/original_data.xls'#输入的数据文件data=pd.read_excel(inputfile)#读取数......