此问题类似于here提出的问题。然而,在这种情况下,海报的最终目的似乎是选择一段文字,他们能够找到一种不涉及点击的解决方法。
与上述问题中的用户一样,我最初认为可以通过三次调用click方法来模拟三次点击。
new Actions(driver)
.moveToElement(svgElement, posX, posY)
.click()
.click()
.click()
.perform()
但是,这不起作用,因为我的javascript代码会检查detail
实例的UIEvent
属性,对1
的每次调用始终为click
}。因此,以下片段:
function clickHandler (event) {
if (event.detail == 1) {
singleClickHandler()
}
if (event.detail == 2) {
doubleClickHandler()
}
if (event.detail == 3) {
tripleClickHandler()
}
导致singleClickHandler
在通过Selenium调用时被调用三次,而singleClickHandler
,doubleClickHandler
和tripleClickHandler
中的每一个在通过手动执行此操作时被调用一次浏览器(Firefox)。
如何通过硒触发detail
等于3的点击事件?
答案 0 :(得分:3)
当前的api没有提供模拟三次点击的方法,这会发出点击次数的单击事件。 因此,您最好的机会是使用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);
答案 1 :(得分:0)
也许您可以尝试下面的内容:
WebElement yourElement = driver.findElement(By.xpath("xpath locator here"));
Actions action = new Actions(driver);
Actions action1= new Actions(driver);
action1= action.moveToElement(yourElement).doubleClick();
action1.click().build().perform();