Reputation: 23
I want to right click on a link and select the first option in context menu.
I tried with below five methods nothing works. all methods are clicking the link in normal way, but not happening context click.
Can somebody throw some light on this what's wrong?
I am using the environment of...selenium-3.4.0, FF 53, Windows 7
package WebDriverAdvancedPrograms;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class RightClick {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.gecko.driver", "G:\\Selenium\\geckodriver-v0.16.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element=driver.findElement(By.linkText("Forgotten account?"));
// // Method 1 - NOT WORKING
// Actions builder = new Actions(driver);
// Action mouseOverHome = builder
// .moveToElement(element)
// .contextClick(element)
// .sendKeys(Keys.DOWN)
// .sendKeys(Keys.ENTER)
// .build();
// mouseOverHome.perform();
// Method 2 - NOT WORKING
// Actions builder = new Actions(driver);
// Action mouseOverHome = builder
// .moveToElement(element)
// .contextClick(element)
// .sendKeys(Keys.ARROW_DOWN)
// .sendKeys(Keys.RETURN)
// .build();
// mouseOverHome.perform();
// Method 3 - NOT WORKING
// Actions builder = new Actions(driver);
// Action mouseOverHome = builder
// .contextClick(element)
// .sendKeys(Keys.ARROW_DOWN)
// .sendKeys(Keys.RETURN)
// .build();
// mouseOverHome.perform();
// Method 4 - NOT WORKING
// Actions builder = new Actions(driver);
// Action mouseOverHome = builder
// .contextClick(element)
// .sendKeys(Keys.ARROW_DOWN)
// .sendKeys(Keys.ARROW_UP)
// .sendKeys(Keys.RETURN)
// .build();
// mouseOverHome.perform();
// // Method 5 - NOT WORKING
Actions action= new Actions(driver);
action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
// Method 6 - Partially WORKING
Point p = element.getLocation();
int x = p.getX();
int y = p.getY();
Robot r=new Robot();
r.mouseMove(x, y);
r.mousePress(InputEvent.BUTTON3_MASK);
r.mouseRelease(InputEvent.BUTTON3_MASK);
r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
//Method 7 - WORKING
Robot r=new Robot();
r.mouseMove(1000, 145);
r.mousePress(InputEvent.BUTTON3_MASK);
r.mouseRelease(InputEvent.BUTTON3_MASK);
r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
}
Method 6 is partially working means... Context click is happening on the screen but not on element.
In Method 7, I entered manually x, y position its working...
The reason for method 6 not working is x, y getting from getLocation method is related to browser, but robot class is working related to screen..
Here my question is how to get exact location of element related to screen
Thanks Ravi M
Upvotes: 1
Views: 4334
Reputation: 4739
use this:
Actions right_action = new Actions(chromeDriver);
right_action.ContextClick(element_name).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();
Upvotes: 0
Reputation: 8541
Please try,
Actions builder = new Actions(driver);
builder.ContextClick(element).Build().Perform();
or please try by changing RETURN to ENTER like below,
Actions action= new Actions(driver);
action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Upvotes: 1