Selenium C# – Context Right Click

Pankaj KumarPankaj Kumar
2 min read

Selenium C# – Context Right Click

Introduction

In Selenium automation, doing a context (right) click can be super helpful when you need to interact with context menus or trigger specific events on elements. Selenium has a handy Actions class that lets you simulate complex user actions, like right-clicks. In this guide, we'll walk you through how to use Selenium to perform a right-click on a web element using C#.

Code Implementation

Below is a C# Selenium script that opens a browser, navigates to a demo website, clicks a link, and performs a right-click on a specific element using the Actions class.

Example: Context Right Click with Selenium in C#.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;

namespace SACSSAuto.QA.SeleniumPractice
{
    [TestClass]
    public class Selenium_78
    {
        /****
         *  Sample Code Definition :- Selenium - Context Right Click
         *  Developed  By :- Pankaj Kumar
         *  Date of Creation :- 31-Jan-2025
         *  Project :- SACSSAUTO - C# [Web / Mobile / API]       *  
         */
        [TestMethod]
        public void Selenium_Test()
        {
            Console.WriteLine("Selenium - Context Right Click");
            // Open Chrome Browser
            IWebDriver driver = new ChromeDriver();
            // Maximize the Browser Window
            driver.Manage().Window.Maximize();
            // Open the URL
            driver.Navigate().GoToUrl("https://practice.solveautomation.in");
            // Wait for 5 seconds
            Thread.Sleep(5000);
            //Click the Register Link
            driver.FindElement(By.XPath("//a[normalize-space()='Register']")).Click();
            // Wait for 5 Seconds
            Thread.Sleep(5000);
            IWebElement element = driver.FindElement(By.XPath("//h1[normalize-space()='Register']"));
            Actions actions = new Actions(driver);
            actions.ContextClick(element).Build().Perform();
            // Wait for 5 seconds
            Thread.Sleep(5000);         
            // Quit the driver
            driver.Quit();
        }
    }
}

Steps Explained

1. Navigating to the Target Page

The browser opens the practice site and clicks the "Register" link.

2. Performing Context Click

The script identifies the "Register" header and uses the Actions class to perform a right-click.

3. Wait and Exit

After the right-click, the script waits briefly and closes the browser session.

Expected Outcome

  • Opens the practice website.

  • Navigates to the Register page.

  • Performs a right-click on the "Register" heading.

  • Closes the browser session.

Conclusion

Using Selenium's Actions class lets you easily mimic user actions like right-clicks. It's super handy for testing context menus or any custom right-click features on web elements.

0
Subscribe to my newsletter

Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pankaj Kumar
Pankaj Kumar