9

How to write selenium java code for doubleClick() on a record using web driver?

I have displayed some records in the body part. Once I clicked on a record we should get a popup window to update it.

Please suggest how to write Selenium Java code using web driver.

I have tried following code:

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().build().perform();
Krunal
  • 77,632
  • 48
  • 245
  • 261
Gokul
  • 233
  • 2
  • 6
  • 12
  • please provide your code for better understanding of the problem – Arpan Buch Feb 20 '14 at 11:54
  • @AAB I have posted the code which I have tried – Gokul Feb 20 '14 at 11:59
  • 1
    ah, please give us html snippet so we can see how your elements look in the page – diazazar Feb 20 '14 at 13:22
  • @paul.cioroianu I have posted the code to the question which i have tried to double click a row on the grid. With this code only the row in the grid is selected but not double clicked. If I double click the row then i will be getting a pop up displayed. Could you please help me? – Gokul Feb 21 '14 at 04:03
  • @Gokul you could just try to call click on it twice. the method findElement will return the element for you, so just call element.click() and see if that works. – diazazar Feb 21 '14 at 07:11

8 Answers8

21

Use Actions class to perform mouse, keyboard actions on WebElements using WebDriver.

Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText("TEST"));

//Double click
action.doubleClick(element).perform();

//Mouse over
action.moveToElement(element).perform();

//Right Click
action.contextClick(element).perform();
Santoshsarma
  • 5,627
  • 1
  • 24
  • 39
  • 1
    I tried with your code to implement this using xpath but it did not worked. I have posted the code to the question which i have tried to double click a row on the grid. With this code only the row in the grid is selected but not double clicked. If I double click the row in the grid then a pop up should be displayed. Could you please help me? – Gokul Feb 21 '14 at 04:09
  • Could you please share your html code here to help you further? Is that link is in iframe ? – Santoshsarma Feb 21 '14 at 05:01
9
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().perform();

This code works!!!

Gokul
  • 233
  • 2
  • 6
  • 12
8

You should use the Actions() class as this includes a 'double-click' action.

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Test"))).doubleClick().build().perform();
Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
4

Try this code:

Actions action = new Actions(driver);
WebElement btnElement=driver.findElement("Locator of element"));
action.doubleClick(btnElement).build().perform();
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
1

You can make use of Actions class of WebDriver to perform composite actions like Double click, Drag and Drop, Hover etc.

// Creates an instance of Actions class, passing current driver instance.

Actions builder = new Actions(driver);

Way 1:

// Gets an Action class object that holds an action/ a set of actions

Action action = builder.doubleClick(element);

// Builds the set of actions/ single action using build() and executes on browser using perform() method.

action.build().perform();

Way 2:

// Calls build() and perform() methods directly on Actions class instance

builder.doubleClick().build().perform();
budi
  • 6,351
  • 10
  • 55
  • 80
0

And in case if have no additional actions binded to singleclick, you can use:

driver.findElement(By.xpath("%youXPath%"))).click;
driver.findElement(By.xpath("%youXPath%"))).click;

Actually, it should works in most cases (except you have some custom system doubleclick settings)

0
  WebElement element = driver.findElement(selector);
  Actions builder = new Actions(driver);
  builder.doubleClick(element).perform();
Ran Adler
  • 3,587
  • 30
  • 27
0

I implemented Ran's (immediately above my post) solution. I'm writing Java in Eclipse and using Selenium WebDriver.

There are 2 imports that you'll need:

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

Then, I implemented the code thusly:

WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[1]/div[3]/div[8]/div[2]/div/div[2]/div/table/tbody/tr[2]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();

Thanks to Ran! I'd been struggling with this one for several hours. Invoking the single click twice doesn't work for me - too much time between the events to be captured by the browser under test as a double-click.

slfan
  • 8,950
  • 115
  • 65
  • 78