使用Selenium的click_and_hold方法时,究竟是什么条件或动作导致鼠标单击释放? [英] When using Selenium's click_and_hold method exactly what conditions or actions cause the mouse click to release?

查看:1488
本文介绍了使用Selenium的click_and_hold方法时,究竟是什么条件或动作导致鼠标单击释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在硒测试中有几次决定使用Selenium的 click_and_hold()此处的源代码)在某些元素上。源代码使它看起来像将无限期地保持按下状态,但是肯定有一些动作(例如简单的单击)会导致释放所保持的单击。显然,调用 release 也会释放保留的点击,但是没有人知道到底是什么操作/条件(来自脚本还是页面本身)会导致保留的点击?

I've had several occasions in my selenium tests where I decided to use Selenium's click_and_hold() (source code here) method on some element. The source code makes it look like it will stay pressed indefinitely but there are definitely some actions, such as a simple click, that cause the held click to be released. Obviously calling release will release the held click too, but does anyone have a grasp on exactly what actions/conditions (either from the script or the page itself) will cause the held click to be released?

我已经链接到python绑定的文档,但是无论使用哪种语言编写脚本,我都认为这是相同的。

I've linked to the documentation for the python bindings, but I assume this would be the same no matter what language is used to write the script. Please let me know if this assumption is incorrect!

推荐答案

release()



释放(on_element) 释放元素上按住的鼠标按钮。如果 on_element None ,则在当前鼠标位置上释放定义为:

release()

release(on_element) releases a held mouse button on an element. If on_element is None releases on current mouse position which is defined as:

def release(self, on_element=None):
    """
    Releasing a held mouse button on an element.

    :Args:
     - on_element: The element to mouse up.
       If None, releases on current mouse position.
    """
    if on_element:
            self.move_to_element(on_element)
    if self._driver.w3c:
        self.w3c_actions.pointer_action.release()
        self.w3c_actions.key_action.pause()
    else:
        self._actions.append(lambda: self._driver.execute(Command.MOUSE_UP, {}))
    return self

release()默认情况下由 ActionChains 实现。其中一些如下:

release() is invoked by default by different methods of the ActionChains implementation. Some of them are as follows:


  • release():释放持有的鼠标

  • drag_and_drop(源,目标):在源元素上按住鼠标左键,然后移至

  • drag_and_drop_by_offset(source,xoffset,yoffset):在鼠标左键上按住鼠标左键。源元素,然后移至目标偏移量并释放鼠标按钮。

  • release(): Releasing a held mouse button on an element.
  • drag_and_drop(source, target): Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
  • drag_and_drop_by_offset(source, xoffset, yoffset): Holds down the left mouse button on the source element, then moves to the target offset and releases the mouse button.

click_and_hold()可以在元素上按住鼠标左键。

click_and_hold() holds down the left mouse button on an element.

所以您看对了,源代码相同,除非 release(),否则它将无限期保持按下状态直接调用或通过其他方法调用。但是,可能还有其他操作 / 事件,可能会导致按住鼠标左键隐式释放。这些操作 / 事件可以是任何 HTML DOM事件。 HTML DOM事件允许 JavaScript / AjaxCalls 在HTML文档中的元素上注册不同的事件处理程序。一些最常见的事件是:

So you saw it right, the source code confirms the same that it will stay pressed indefinitely unless release() is directly invoked or invoked through other methods. However there can be other actions / events which may cause the hold down left mouse button to be released implicitly. These actions / events can be a result of any of the HTML DOM Events. HTML DOM events allow JavaScript / AjaxCalls to register different event handlers on elements in an HTML document. Some of the mostly encountered events are:


  • HTML DOM UiEvent :从用户界面触发的事件属于UiEvent对象。


    • 加载事件:onload事件在对象被加载时发生。

    • onresize事件:当调整浏览器窗口的大小时会发生onresize事件。

    • onscroll事件:当滚动元素的滚动条时,发生onscroll事件。

    • HTML DOM UiEvent: Events that are triggered from the user interface belongs to the UiEvent Object.
      • onload Event: The onload event occurs when an object has been loaded.
      • onresize Event: The onresize event occurs when the browser window has been resized.
      • onscroll Event: The onscroll event occurs when an element's scrollbar is being scrolled.
      • onblur Event: The event occurs when an element loses focus
      • onfocus Event: The event occurs when an element gets focus
      • onfocusin Event: The event occurs when an element is about to get focus
      • onfocusout Event: The event occurs when an element is about to lose focus

      • onchange事件:当元素的值已更改时,将发生onchange事件。

      • onchange Event: The onchange event occurs when the value of an element has been changed.

      • onmousedown事件:当用户在元素上按下鼠标按钮时,会发生onmousedown事件。

      • onmouseup事件:当用户在元素上释放鼠标按钮时,会发生onmouseup事件。

      • onmousedown Event: The onmousedown event occurs when a user presses a mouse button over an element.
      • onmouseup Event: The onmouseup event occurs when a user releases a mouse button over an element.

      • ondrag事件:ondrag事件在拖动元素或文本选择时发生。

      • ondragstart事件:当用户开始拖动元素或文本选择时,就会发生ondragstart事件。

      • ondrag Event: The ondrag event occurs when an element or text selection is being dragged.
      • ondragstart Event: The ondragstart event occurs when the user starts to drag an element or text selection.
      • transitionend Event: The event occurs when a CSS transition has completed

      key_up() 方法还会释放修饰键。例如:

      ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
      






      此用例



      对于实现 click_and_hold()用例没有任何可见性即, key_down()是一种执行修改键的操作的方法,它不会释放修改键,并且随后的交互可能会假定一直处于按下状态。请注意,修饰键从不隐式释放 key_up(theKey) send_keys(Keys.NULL)必须调用才能释放修饰符。


      This usecase

      Without any visibility to your usecase of implementing click_and_hold() it is to be noted that, key_down() is a method which performs a modifier key press and it does not release the modifier key and subsequent interactions may assume it's kept pressed. Note that the modifier key is never released implicitly. Either key_up(theKey) or send_keys(Keys.NULL) must be called to release the modifier.

      这篇关于使用Selenium的click_and_hold方法时,究竟是什么条件或动作导致鼠标单击释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆