selenium学习拖拽页面元素

一、ActionChains包

模拟鼠标的操作要首先引入ActionChains的包


from selenium.webdriver.common.action_chains import ActionChains
而对于ActionChains包,一般的写法是:


这是这个方法一般的书写格式,下面我们来看一如何使用模拟鼠标操作的具体案例

1.鼠标拖动操作

方法:

drag_and_drop(source, target) 

拖动source元素到target元素的位置

drag_and_drop_by_offset(source, xoffset, yoffset)

source:鼠标拖动的原始元素

xoffset:鼠标把元素拖动到另外一个位置的x坐标

yoffset:鼠标把元素拖动到另外一个位置的y坐标

拖动source元素到指定的坐标
————————————————
版权声明:本文为CSDN博主「许西城」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ccggaag/article/details/75717186

二、代码示例

测试网址:https://jqueryui.com/resources/demos/draggable/scroll.html

 

# coding=UTF-8
#17.拖拽页面元素
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from selenium import webdriver
import unittest
import time
from selenium.webdriver import ActionChains

class Case17(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_dragPageElement(self):
        url = "https://jqueryui.com/resources/demos/draggable/scroll.html"
        self.driver.get(url)
        position1 = self.driver.find_element_by_id("draggable")
        position2 = self.driver.find_element_by_id("draggable2")
        position3 = self.driver.find_element_by_id("draggable3")
        ActionChains(self.driver).drag_and_drop(position1,position2).perform() #把position1拖到position2的位置
        time.sleep(2)
        ActionChains(self.driver).drag_and_drop_by_offset(position3,10,10).perform() #把position3拖动(10,10)的距离,即向右下方拖动
        time.sleep(2)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
原文地址:https://www.cnblogs.com/erchun/p/11778511.html