Friday, 21 February 2014

How to double click on the web element in Selenium Webdriver in Java?


We can perform all kinds of mouse and keyboard events in selenium webdriver using Actions interface.

Please note that we need to import below classes/interfaces to perform below operations.
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

Java Example to double click on Webelement in Selenium Webdriver

package temp;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;



@SuppressWarnings("unused")
public class OpenGoogle {
   
public static void main(String [] arg)
{

System.setProperty("webdriver.chrome.driver""C:\\SelenuimProject\\chromedriver2.8.exe");
WebDriver driver =  new ChromeDriver();

try{
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

driver.get("http://www.google.com/");

Thread.sleep(3000);

WebElement element = driver.findElement(By.id("hplogo"));

Actions builder = new Actions(driver);

//build the action chain.
Action doubleclick = builder.doubleClick(element).build();

//perform the double click action
doubleclick.perform();


Thread.sleep(8000);


}

catch(Exception e){
       System.out.println("Exception - > " + e.toString());
       }
       finally{
              driver.close();
              driver.quit();
       }
}      //main function ends
      
}//class ends


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

4 comments:

  1. Hi Sagar
    Your blog really helped me
    Thanks

    ReplyDelete
  2. Hello Sagar
    Your blogs are of gr8 help .
    Thank you !

    ReplyDelete
  3. Really a very nice explanation.. thanks for your help

    ReplyDelete
  4. Nice one and Can we have same one for mobile automation...

    ReplyDelete

Buy Best Selenium Books

Contributors