Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

In Selenium WebDriver, performing mouse actions like right-click (context click) can be done using the ‘Actions‘ class. The ‘Actions‘ class provides a way to perform more complex user interactions than the basic operations like clicking or typing.

Steps to Perform Right Click (Context Click) in Selenium WebDriver

  • Set Up WebDriver and Navigate to the Page: Initialize the WebDriver and navigate to the desired web page.

  • Locate the Element: Find the element on which you want to perform the right-click action.

  • Create an Actions Object: Instantiate the ‘Actions‘ class by passing the WebDriver instance to its constructor.

  • Perform the Right Click Action: Use the ‘contextClick()‘ method of the ‘Actions‘ class to perform the right-click action on the located element.

  • Build and Perform the Action: Use the ‘build().perform()‘ methods to execute the action.

Example Code

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class RightClickExample {
    public static void main(String[] args) {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to the desired web page
        driver.get("https://www.example.com");

        // Locate the element to right-click on
        WebElement elementToRightClick = driver.findElement(By.id("element-id"));

        // Create an instance of the Actions class
        Actions actions = new Actions(driver);

        // Perform the right-click action
        actions.contextClick(elementToRightClick).build().perform();

        // Perform additional actions as needed (optional)
        // e.g., clicking on a context menu option
        // WebElement contextMenuOption = driver.findElement(By.id("context-menu-option-id"));
        // contextMenuOption.click();

        // Close the browser
        driver.quit();
    }
}

				
			

Explanation

1. Set the path to the chromedriver executable:

				
					System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

				
			

This line sets the system property to specify the path to the ChromeDriver executable.

2. Initialize WebDriver:

				
					WebDriver driver = new ChromeDriver();

				
			

This initializes the WebDriver for Chrome.

3. Navigate to the desired web page:

				
					driver.get("https://www.example.com");

				
			

This navigates the browser to the specified URL.

4. Locate the element to right-click on:

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

				
			

This locates the element on the web page using its ID. You can use other locators like ‘By.name‘, ‘By.xpath‘, ‘By.cssSelector‘, etc.

5. Create an instance of the Actions class:

				
					Actions actions = new Actions(driver);

				
			

This creates an instance of the ‘Actions‘ class, passing the WebDriver instance to its constructor.

6. Perform the right-click action:

				
					actions.contextClick(elementToRightClick).build().perform();

				
			
  • contextClick(elementToRightClick)‘: Specifies the element to right-click on.
  • build()‘: Builds the sequence of actions.
  • perform()‘: Executes the action

7.Perform additional actions as needed (optional):

				
					WebElement contextMenuOption = driver.findElement(By.id("context-menu-option-id"));
contextMenuOption.click();

				
			

If you need to interact with the context menu options, locate the menu option and perform the desired action (like clicking).

8. Close the browser:

				
					driver.quit();

				
			

This closes the browser and ends the WebDriver session.

Advanced Example: Handling Context Menu Options

Here’s an advanced example where we perform a right-click and then select an option from the context menu:

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.List;

public class RightClickContextMenuExample {
    public static void main(String[] args) {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to the desired web page
        driver.get("https://swisnl.github.io/jQuery-contextMenu/demo.html");

        // Locate the element to right-click on
        WebElement elementToRightClick = driver.findElement(By.cssSelector(".context-menu-one"));

        // Create an instance of the Actions class
        Actions actions = new Actions(driver);

        // Perform the right-click action
        actions.contextClick(elementToRightClick).build().perform();

        // Wait for the context menu to be displayed
        try {
            Thread.sleep(1000); // Add an explicit wait for demo purposes
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Locate the context menu options
        List<WebElement> contextMenuOptions = driver.findElements(By.cssSelector(".context-menu-item"));

        // Print the text of each context menu option
        for (WebElement option : contextMenuOptions) {
            System.out.println(option.getText());
        }

        // Click on a specific context menu option (e.g., 'Edit')
        for (WebElement option : contextMenuOptions) {
            if (option.getText().equals("Edit")) {
                option.click();
                break;
            }
        }

        // Close the browser
        driver.quit();
    }
}

				
			

In this example, we navigate to a demo page, perform a right-click on a specific element, and then select an option from the context menu. The ‘Thread.sleep(1000)‘ is used for demonstration purposes to wait for the context menu to appear; in a real-world scenario, you should use explicit waits.

By using the ‘Actions‘ class, you can perform a wide range of complex user interactions in Selenium WebDriver, making your tests more robust and reflective of actual user behavior.

Scroll to Top