iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >selenium drag_and_drop不生效的解决办法
  • 786
分享到

selenium drag_and_drop不生效的解决办法

selenium drag_and_drop不生效selenium drag_and_drop 2023-03-19 17:03:53 786人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录方法1:分解drag_and_drop动作 方法2:使用seletools解决做自动化时发现用drag_and_drop模拟拖拽没效果,页面上只能看到元素so

自动化时发现用drag_and_drop模拟拖拽没效果,页面上只能看到元素source闪了一下,但是并没有拖拽到元素target上(推测可能是我用系统页面在拖拽时有个js效果,但是drag_and_drop模拟拖拽的时候执行太快没能触发JS,所以没有把这两个元素拖拽到一起)。

通过不断尝试,终于解决了,这里记录一下,希望其他人遇到类似情况时能有所启发。方法1是我尝试的过程;方法2是我看到的另一种方法,虽然试验了下没效果,但说不定对其他的拖拽场景是有效的。

方法1:分解drag_and_drop动作

源码可以看出drag_and_drop的源码执行了两个操作,既然直接用drag_and_drop不行,那调整下这两个操作或许可行

    def drag_and_drop(self, source, target):
        """
        Holds down the left mouse button on the source element,
           then moves to the target element and releases the mouse button.
        :Args:
         - source: The element to mouse down.
         - target: The element to mouse up.
        """
        self.click_and_hold(source)
        self.release(target)
        return self

drag_and_drop里有两个动作:click_and_hold(在source元素上单击鼠标不松开),release(在target元素上释放点击状态的鼠标)。中间加一个鼠标移动的动作是否可行呢?

我把拖拽的流程改成了:

        ActionChains(self.driver).click_and_hold(source).perfORM()       
        ActionChains(self.driver).move_by_offset(x, y).perform()
        ActionChains(self.driver).release(target).perform()

试验了一下,在执行move_by_offset动作的时候能触发JS的效果,只不过位移的xy不准确,触发不了另一个JS,只要计算好要偏移的位置就好了

最终的实现:

    def drag_and_drop(self):
        source = self.find_element_and_scroll_into_view(source_loc)
        target = self.find_element_and_scroll_into_view(target_loc)
        # 先移动一点 触发js效果 触发后元素变小 重新获取元素以便能准确计算鼠标偏移量
        ActionChains(self.driver).click_and_hold(source).move_by_offset(5, 0).perform()
        drag_source = self.find_element(change_source_loc)
 
        x1, x2 = (drag_source.location.get("x"), drag_source.location.get("x") + drag_source.size.get("width"))
        y1, y2 = (drag_source.location.get("y"), drag_source.location.get("y") + drag_source.size.get("height"))
        source_middle_x = (x1 + x2) / 2
        source_middle_y = (y1 + y2) / 2
        x3, x4 = (target.location.get("x"), target.location.get("x") + target.size.get("width"))
        y3, y4 = (target.location.get("y") + 0.5 * target.size.get("height"), target.location.get("y") + target.size.get("height"))
        target_middle_x = (x3 + x4) / 2
        target_middle_y = (y3 + y4) / 2
        x = target_middle_x - source_middle_x
        y = target_middle_y - source_middle_y
 
        ActionChains(self.driver).move_by_offset(x, y).perform()
        ActionChains(self.driver).release(target).perform()

 拖拽效果:

 方法2:使用seletools解决

虽然我试了下没效果,但是感觉是有用的,这里一并记录下。

selenium的drag_and_drop方法在某些场景下无效,这是官方很久就已经知道的BUG,只不过没有在源码中修复,而是提供了单独的包,因为David Burnes(核心 Selenium 提交者)认为拖放错误是一个WEBdriver网络驱动问题,在Selenium中提供任何暂时避开网络的方法并不是一个好主意。详细内容可以阅读文章「Selenium Drag and Drop Bug Workaround」

安装

pip install seletools

使用

from seletools.actions import drag_and_drop
 
source = driver.find_element(xxx)
target = driver.find_element(xxx)
drag_and_drop(driver, source, target)

到此这篇关于selenium drag_and_drop不生效的解决办法的文章就介绍到这了,更多相关selenium drag_and_drop不生效内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: selenium drag_and_drop不生效的解决办法

本文链接: https://www.lsjlt.com/news/200414.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作