温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在python项目中使用selenium实现一个鼠标拖拽功能

发布时间:2020-12-24 16:02:25 来源:亿速云 阅读:538 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关怎么在python项目中使用selenium实现一个鼠标拖拽功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

首先我们需要准备一个鼠标滑动的html文件,用来演示鼠标滑动的效果,注意需要将我们的html文件放在自己的服务器上,

html文件如下:

<html> <head>   <meta charset="utf-8" />   <style>     body {   margin: 0;   padding: 0; } input{   appearance:none;   -moz-appearance:none;   -webkit-appearance:none;   background: none;   border:none; } .wrap{   margin: 200px 0 0 200px; } .box {   position: relative;   width: 200px;   height: 30px;   border-radius: 20px;   background: #686B69;   line-height: 30px;   overflow: hidden;   margin-bottom: 40px;   color: #fff;   font-size: 12px; } .btn {   position: absolute;   top: 0;   left: 0;   height: 30px;   width: 30px;   background: #0c7;   border-radius: 20px;   text-align: center; } .tips {   text-align: center; } #submit{   line-height: 28px;   border-radius: 3px;   background: #0c7;   width: 200px;   text-align: center;   color: #fff; }   </style> </head> <body> <div class="wrap">   <div class="box">     <div class="btn" id="dragEle"></div>     <div class="tips">>>拖动滑块验证<<</div>   </div>  <input type="button" value="提交验证" id="submit" /> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script type="text/javascript">   function DragValidate (dargEle,msgEle){     var dragging = false;//滑块拖动标识     var iX;     dargEle.mousedown(function(e) {       msgEle.text("");       dragging = true;       iX = e.clientX; //获取初始坐标     });     $(document).mousemove(function(e) {       if (dragging) {         var e = e || window.event;         var oX = e.clientX - iX;         if(oX < 30){           return false;         };         if(oX >= 210){//容器宽度+10           oX = 200;           return false;         };         dargEle.width(oX + "px");         //console.log(oX);         return false;       };     });     $(document).mouseup(function(e) {       var width = dargEle.width();       if(width < 200){         //console.log(width);         dargEle.width("30px");         msgEle.text(">>拖动滑块验证<<");       }else{         dargEle.attr("validate","true").text("验证成功!").unbind("mousedown");       };       dragging = false;     });   };   DragValidate($("#dragEle"),$(".tips"));   $("#submit").click(function(){     if(!$("#dragEle").attr("validate")){       alert("请先拖动滑块验证!");     }else{       alert("验证成功!");     }   }); </script> </body> </html>

2、使用selenium进行鼠标拖拽操作,具体代码如下:

from selenium import webdriver import unittest from selenium.webdriver import ActionChains import time     url = 'http://192.168.62.9:1234/easytest/tt' driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.get(url) driver.maximize_window()  # 获取第一,二,三能拖拽的元素 drag1 = driver.find_element_by_id('dragEle')   # 创建一个新的ActionChains,将webdriver实例对driver作为参数值传入,然后通过WenDriver实例执行用户动作 action_chains = ActionChains(driver) # 将页面上的第一个能被拖拽的元素拖拽到第二个元素位置 # 将页面上的第三个能拖拽的元素,向右下拖动10个像素,共拖动5次 action_chains.drag_and_drop_by_offset(drag1, 208, 0).perform() time.sleep(5) driver.quit()

关于怎么在python项目中使用selenium实现一个鼠标拖拽功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI