How can I write Selenium WebDriver Java code to perform a double-click action on a record in a grid?

9    Asked by joel_6452 in QA Testing , Asked on Jan 15, 2025

In the body section of the grid, I have a number of records displayed. If a record is double-clicked, a specific pop-up window comes on the screen to allow the updating of the record. Using WebDriver, I need to write Selenium Java code so as to automate this double click action.

To perform a double-click action on a record in a grid using Selenium WebDriver in Java, you can use the Actions class, which provides support for advanced user interactions like double-clicking. Here's an example solution:

Working Code Example:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.chrome.ChromeDriver;
public class DoubleClickExample {
    public static void main(String[] args) {
        // Set up WebDriver (example using ChromeDriver)
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            // Navigate to the webpage
            driver.get("https://example.com"); // Replace with your target URL
            // Locate the element in the grid (adjust the XPath as per your DOM structure)
            WebElement record = driver.findElement(By.xpath("//table/tbody/tr[2]"));
            // Perform the double-click action on the located record
            Actions actions = new Actions(driver);
            actions.doubleClick(record).perform();
            // Optionally, handle the popup window
            // Wait for the popup to appear (adjust timeout as necessary)
            WebElement popup = driver.findElement(By.id("popupElementId")); // Replace with your popup element's ID
            System.out.println("Popup is displayed: " + popup.isDisplayed());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

Explanation:

 Set up WebDriver:

  • Ensure that the appropriate WebDriver is configured (e.g., chromedriver for Chrome).

Locate the Grid Element:

  • Use driver.findElement with the correct XPath or CSS selector to locate the specific row in the grid. Replace the XPath //table/tbody/tr[2] with the actual structure of your HTML.

Perform Double-Click Action:

  • Use the Actions class to perform the doubleClick() method on the located element.

Handle Popup:

  • After double-clicking, if a popup appears, use Selenium to locate and interact with the popup elements.

Clean Up:

  • Always close the browser using driver.quit() to release resources.

Notes:

  • Replace path/to/chromedriver with the actual path to your WebDriver executable.
  • Adjust the XPath, element IDs, and selectors to match your application's HTML structure.
  • Add explicit waits if the popup or elements take time to appear.

If you provide more details about the grid or popup structure, I can refine the code further.



Your Answer

Interviews

Parent Categories