Triple click using selenium

十年热恋 提交于 2021-02-08 06:44:26

问题


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.

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()

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() 
    }

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).

How do I cause a click event with detail equal to 3 to be triggered via selenium?


回答1:


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);



回答2:


Maybe you could try something like below:

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();


来源:https://stackoverflow.com/questions/44330194/triple-click-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!