使用硒三次 [英] Triple click using selenium
问题描述
此问题类似于在此处一个>.但是,在那种情况下,海报的最终目的似乎实际上是选择一段文本,他们能够找到一种不涉及单击的变通方法.
This question is similar to the one asked here. However in that case it seems that the poster's ultimate purpose was actually to select a paragraph of text, and they were able to find a work-around which did not involve clicking.
像上面问题中的用户一样,我最初认为可以通过三次单击click方法来模拟三次单击.
Like the user in the above question I initially thought that it would be possible to simulate a triple-click by calling the click method three times.
new Actions(driver)
.moveToElement(svgElement, posX, posY)
.click()
.click()
.click()
.perform()
但是,这不起作用,因为我的JavaScript代码检查了 UIEvent
实例的 detail
属性,并且始终为 1
每次调用 click
.因此,以下代码段:
However, this doesn't work as the my javascript code checks the the detail
attribute of the UIEvent
instance, and this is always 1
for each call to click
. Thus the following snippet:
function clickHandler (event) {
if (event.detail == 1) {
singleClickHandler()
}
if (event.detail == 2) {
doubleClickHandler()
}
if (event.detail == 3) {
tripleClickHandler()
}
导致通过Selenium调用 singleClickHandler
三次,而 singleClickHandler
, doubleClickHandler
和 tripleClickHandler 通过浏览器(Firefox)手动执行一次code>时就会被调用.
causes singleClickHandler
to be called three times when called via Selenium, whereas each of singleClickHandler
, doubleClickHandler
, and tripleClickHandler
is called once when this is exercised manually via the browser (Firefox).
如何通过硒触发 detail
等于3的点击事件?
How do I cause a click event with detail
equal to 3 to be triggered via selenium?
推荐答案
当前api没有提供一种模拟三次点击的方法,该方法会发出带有点击计数的一次点击事件.因此,您最好的机会可能是使用executeScript模拟事件:
The current api doesn't provide a way to simulate a triple click which would emit a single click event with the count of clicks. So your best chance is likely to simulate the event with executeScript:
String JS_CLICK_TRIPLE =
"var target = arguments[0]; " +
"var offsetX = arguments[1]; " +
"var offsetY = arguments[2]; " +
"var rect = target.getBoundingClientRect(); " +
"var cx = rect.left + (offsetX || (rect.width / 2)); " +
"var cy = rect.top + (offsetY || (rect.height / 2)); " +
" " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('click', {clientX: cx, clientY: cy, detail: 3}); " +
" " +
"function emit(name, init) { " +
"target.dispatchEvent(new MouseEvent(name, init)); " +
"} " ;
Actions action1 = new Actions(driver);
action1.moveToElement(yourElement, posX, posY).perform();
((JavascriptExecutor)driver).executeScript(
JS_CLICK_TRIPLE, yourElement, posX, posY);
这篇关于使用硒三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!