0

I am not able to click on any element after doing right click in selenium(Java). Its just doing a right click and is not clicking of any of the options like open in new tab...Instead its just doing a normal click after doing a right click.Can anyone please help me.Below is my code

System.setProperty("webdriver.chrome.driver","C:\Selenium\chromedriver.exe"); WebDriver wd=new ChromeDriver();

    wd.get("http://google.com");
    Thread.sleep(3000); 
    //Point a=wd.findElement(By.linkText("Testing")).getLocation();
    WebElement b=wd.findElement(By.linkText("About"));
    Actions action=new Actions(wd);

    //action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
    action.moveToElement(b);
    Thread.sleep(4000);
    //action.contextClick(b);
    action.contextClick(b);
    action.sendKeys(Keys.ARROW_DOWN).sendKe ys(Keys.ENTER).build().perform();

I have tried via context click and move to element as well but no result.Thanks in advance..

1
  • Ignore the spelling mistake in sendKeys command in last line.. Commented Feb 17, 2016 at 17:33

1 Answer 1

0

Might be this is what you want:

To select the item from the contextual menu, you have to just move your mouse positions with the use of Key down event like this:-

Actions action= new Actions(driver);
action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();

Soure: Select an Option from the Right-Click Menu in Selenium Webdriver - Java

As per request in the comments adding alternative way to open a link in new tab.

    System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
    WebDriver driver= new ChromeDriver();
    driver.get("http://www.google.com/");
    WebDriverWait wait = new WebDriverWait(driver, 30);
    Thread.sleep(3000); 
    WebElement b=driver.findElement(By.linkText("About"));
    Actions action=new Actions(driver);
    action.moveToElement(b).perform();
    Thread.sleep(4000);
    action.keyDown(Keys.CONTROL);
    action.click();
    action.keyDown(Keys.CONTROL).build().perform();
    //action.sendKeys(Keys.RETURN).perform();
    Thread.sleep(4000);
    driver.quit();

The build() method is used compile all the listed actions into a single step. We use build() when we are performing sequence of operations. We can directly use perform() if we are performing single action. You can read more about Actions class.

Also, re-iterating that I was unable to find the root of the issue in the limited time I had, so I placed a work around. So like we use the shortcut CTRL+CLICK to open the link in new tab manually, You might need to find out the shortcut for what you need, you can refer this : https://support.google.com/chrome/answer/157179?hl=en

Hope it helps :)

Sign up to request clarification or add additional context in comments.

21 Comments

It is the same as my code.Even tried with this but no result.What it actually doing is just showing me a right click and not going further.I want to click on the options after right click.
Can anyone please let me know how to do this??or if thr is any other way to achieve the desired result??
I tried the exact same code which you provided, it's working perfectly fine for me. Opening the link in a new tab.
May be it would help if you provide what's the behavior that you are seeing in detail.
Then how come the code is not working for me.I am new to selenium so not able to understand the issue.Its like it shows me that right click has happened.After that it will click (left click)on the about tab(Which i shared in code) i.e. the right click is just shown and its doing a normal click on the specified element.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.