• 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()
  • 相关阅读:
    20150805-20150807 tradeDate-----python
    nutz_web应用中主页跳转到登录页面的方式
    nutz中实现登录验证
    C#之继承
    C#中Page执行顺序:OnPreInit()、OnInit()……
    利用堆栈实现走迷宫算法
    对数组结构体按照K值翻转
    实现多项式的加法和乘法运算
    两个有序链表的合并
    队列的比较和存储方式
  • 原文地址:https://www.cnblogs.com/erchun/p/11778511.html
Copyright © 2020-2023  润新知