0
votes

How to select a right click operation on a link or image and choose any option?

I know how to do it using SendKeys with below code:

WebElement o_Mail=driver.findElement(By.xpath("/html/body/center/form/table/tbody/tr/td[3]/a"));
Actions action=new Actions(driver);
action.contextClick(o_Mail).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();

The drawback is here we should know how many times the DOWN button should be pressed.

But how can i select with option name which will be like below?

action.contextClick(o_Mail).perform();
WebElement o_element=driver.findElement(By.xpath("//option[contains(text(),'Open Link in New Tab')]"));
o_element.click();

You can use google main page and right click on Advance search and choose Open Link in New Tab?

Please help.

4

4 Answers

1
votes

If you want 2 separate windows, what you can do is grab the url of the file (by getting the href attribute of the element) then start a new driver and navigate to that link.

1
votes

I've never coded selenium scripts in Java but something like this might work:

WebElement o_element=driver.findElement(By.xpath("//option[contains(text(),'Open Link in New Tab')]"));
String url = o_element.getAttribute("href");
driver.executeScript("window.open(" + url + ", '_blank');");
0
votes

Just a guess: Opening link in new tab is also mostly done by Ctrl + click

I did not do that yet in my selenium tests, but you could do this the same way you do the key down approach...

0
votes

If 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. Below code will select the second option from contextual menu:-

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

hope this will works for you.