栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 基于selenium实现鼠标拖拽功能

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 基于selenium实现鼠标拖拽功能

1、准备html文件

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

这样我们才能够通过selenium来进行验证。html文件如下:



  
  
    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;
}
  



  
    
    >>拖动滑块验证<<
  
 




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实现鼠标拖拽功能的详细内容,更多关于python 鼠标拖拽的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/15814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号