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..

Prateek
  • 1,145
  • 1
  • 8
  • 21
dheeraj kateja
  • 161
  • 2
  • 4
  • 13

1 Answers1

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 :)

Community
  • 1
  • 1
Prateek
  • 1,145
  • 1
  • 8
  • 21
  • 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. – dheeraj kateja Feb 17 '16 at 17:50
  • Can anyone please let me know how to do this??or if thr is any other way to achieve the desired result?? – dheeraj kateja Feb 18 '16 at 03:53
  • I tried the exact same code which you provided, it's working perfectly fine for me. Opening the link in a new tab. – Prateek Feb 18 '16 at 04:34
  • May be it would help if you provide what's the behavior that you are seeing in detail. – Prateek Feb 18 '16 at 04:37
  • 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. – dheeraj kateja Feb 18 '16 at 04:59
  • There must be something in your code which i may have missed,otherwise is it possible that the code may not work on some machines.Are there any addition settings too be done and i am using it in google chrome. – dheeraj kateja Feb 18 '16 at 05:02
  • Does the context menu opens? – Prateek Feb 18 '16 at 05:07
  • .It opens google.com and then shows me a right click on about tab.After that its clicking on the about tab instead of clcking on open in new tab.. – dheeraj kateja Feb 18 '16 at 05:12
  • i just checked the same code in firefox and its working as expected.I think the issue is with chrome browser.Can anyone suggest are thr any seetings to be done in chrome? – dheeraj kateja Feb 18 '16 at 07:27
  • Hi Dheeraj, I tried it on Chrome it was working for me. Can't understand what's happening in your case.. – Prateek Feb 18 '16 at 07:44
  • Suppose in real scenario after doing a right click,the arrow keys cant select the options shown,then how we can automate this. – dheeraj kateja Feb 18 '16 at 09:21
  • You can use other options/shortcuts available. Like for opening something in new tab, we have CTRL + Click. You can use: `action.keyDown(Keys.CONTROL);` `action.click();` `action.keyDown(Keys.CONTROL).build().perform();` – Prateek Feb 18 '16 at 09:30
  • How to use control+click.Can you tell me the full command for it.Also,i want to use control+arrow down keys to select multiple mails in outllook mail application.Can u tell me how can we achieve that? – dheeraj kateja Feb 18 '16 at 14:30
  • This is my complete code for both mozila and chrome: – dheeraj kateja Feb 18 '16 at 14:51
  • I have already mentioned the code above, are you unable to see it? action.keyDown(Keys.CONTROL); action.click(); action.keyUp(Keys.CONTROL).build().perform(); By the way I am unable to see your code. – Prateek Feb 18 '16 at 15:37
  • Talking about selection multiple elements, you can use, `action.keyDown(Keys.CONTROL);` and `action.keyUp(Keys.CONTROL);` and click individual items within that. – Prateek Feb 18 '16 at 15:38
  • Can u give ur complete code.I am not able to paste the code here due to some issues.Can u put your complete code here which is working. – dheeraj kateja Feb 18 '16 at 16:40
  • Thanks bro.Ur code is working but i am not able to understand what keys and sendkeys command with action.Also keys.control and keys.click.There is click() followed by build.perform().Can you explain your code and also how we can use the same to select other right click options as well.. – dheeraj kateja Feb 19 '16 at 15:02
  • Also,i want to see the code using sendkeys to select other options for right click other than opening new tab.This code i understood using shortcuts but what if i have to select other options in right click.So i am still tryng to find out why my code is not working in chrome.So,can u share the complete code – dheeraj kateja Feb 19 '16 at 17:30
  • I have updated the detail. I have already shared the complete code, all you need to do is to copy into your main() and fix imports. Let me know if there's any more info you need. Also, whenever I get sufficient time, I'll dig into that chrome issue for sure. – Prateek Feb 20 '16 at 04:09
  • Yes i got ur code and its working.But i am asking the ideal code i.e in which u can use sendkeys to toggle between different right click options.It seems in chrome its not identifying the keys.arrowdown and so on. – dheeraj kateja Feb 20 '16 at 05:55