当使用 Selenium 的 click_and_hold 方法时,究竟是什么条件或动作导致鼠标点击释放? [英] When using Selenium's click_and_hold method exactly what conditions or actions cause the mouse click to release?
问题描述
在我的 selenium 测试中,我有好几次决定使用 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()
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(source, target)
:在源元素上按住鼠标左键,然后移动到目标元素并释放鼠标按钮.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 事件:加载对象时发生 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 事件:当元素失去焦点时发生该事件
- onfocus 事件:当元素获得焦点时发生该事件
- onfocusin 事件:当元素即将获得焦点时发生该事件
- onfocusout 事件:当元素即将失去焦点时发生该事件
- 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 事件:当 CSS 过渡完成时发生该事件
- 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()
<小时>
这个用例
您的 usecase 没有任何可见性实现
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屋!