Right Click on WebElement in Selenium
Here are 2 ways of perform a right click on WebElement in Selenium automation.
- use Actions
- 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