RightClick on WebElement in Selenium

Right Click on WebElement in Selenium

Here are 2 ways of perform a right click on WebElement in Selenium automation.

  1. use Actions
  2. use Javascript

Here is a code example in C#:

public static void RightClickOpenInNewTab(this IWebElement element, IWebDriver driver)
        {
        	// use Actions to click
            var newTab = new Actions(driver);
            newTab.KeyDown(Keys.Control).Click(element).KeyUp(Keys.Control).Build().Perform();
			
			
			// use Javascript to click
            const string strJavaScript = "var element = arguments[0];"
                                         + "var mouseEvent = new MouseEvent(\"click\",{\"button\": 1, \"which\": 1});"
                                         + "element.dispatchEvent(mouseEvent);";
            ((IJavaScriptExecutor)driver).ExecuteScript(strJavaScript, element);
        }

Reference:
[1]: https://stackoverflow.com/questions/41417382/python-selenium-mouse-scroll-wheel-click
[2]: https://stackoverflow.com/questions/6157929/how-to-simulate-a-mouse-click-using-javascript

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章