How to perform multiple clicks on a button (4 times) in selenium webdriver,

How to perform multiple clicks on a button (4 times) in selenium webdriver, without making use of for loop? Currently I have to make use of for loop to click on a button and make it work because on a single click it doesn’t work.

Ashwin Train Asked on December 9, 2014 in Selenium WebDriver.
Add Comment
1 Answer(s)

Hello Ashwin

I am not sure if i understand your requirement correctly but replacement of for loop is recursion , you use that else for double clicking on element you can use Javascript or Action class of Selenium

/***
* Double click on WebElement using JavaScript or Actions Class
*
* @param element
* :Element on which Double click needs to be performed
* @param clickStrategy
* : double click using javascript or using action class
* @param driver
* @author kapilA
*/
public static void doubleClick(WebElement element, WebDriver driver,
    CLICK_STRATEGY clickStrategy) {
    switch (clickStrategy) {
    case USING_ACTION:
    Actions action = new Actions(driver);
    action.doubleClick(element).perform();
    break;
    case USING_JS:
    ((JavascriptExecutor) driver)
    .executeScript(
    "var evt = document.createEvent('MouseEvents');"
    + "evt.initMouseEvent('dblclick',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);"
    + "arguments[0].dispatchEvent(evt);",
    element);
    break;
    default:
    String clickStrategyParameter = "";
    try {
        clickStrategyParameter = clickStrategy.toString();
        } catch (Exception e) {
            clickStrategyParameter = "null";
        }
        LOGGER.error("Parameter missmatch: Unknown click strategy. "
        + clickStrategyParameter);
    }
}
kapilag Supporter Answered on December 9, 2014.

Hi Kapil,
I tried the action class before too but it is not working. Any solution for this??

on December 9, 2014.

What about the javascript code as well in the pasted method , it is having two code with filter as ClickStrategey ,

Simply provide your WebElement to this function and then try

 ((JavascriptExecutor) driver)
    .executeScript(
    "var evt = document.createEvent('MouseEvents');"
    + "evt.initMouseEvent('dblclick',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);"
    + "arguments[0].dispatchEvent(evt);",
    element);
on December 9, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.