How to perform right click on an element and open it a new window using Web Driver?

+1 vote
How to perform right click on an element and open it a new window using Web Driver?
Jul 5, 2019 in Selenium by Karthik
1,633 views

1 answer to this question.

0 votes

Hey Karthik, you can perform right click on an element and open it in new window by using Actions class and getWindowHandle methods in Selenium Webdriver. Following code sample automate the same task:

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class RightClick {

    @Test
    public void method1() {

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://www.edureka.co");
        System.out.println(driver.getTitle());
        Actions action = new Actions(driver);
        WebElement element = driver.findElement(By.partialLinkText("Corporate Training"));

 action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

        Set<String> winid = driver.getWindowHandles();
        Iterator<String> iter = winid.iterator();
        iter.next();

        String tab = iter.next();
        driver.switchTo().window(tab);
        System.out.println(driver.getTitle());

        driver.quit();
    }
}
answered Jul 7, 2019 by Hansraj
How to do this without using Actions class,  i am running test in safari it doesn't support Actions. can we have alternative way to implement this in Java script?
Hi,

I used the above code, but instead of opening on new window, it just opens on the current window

Here is the piece of code in the URL: "https://www.ndtv.com/business"

// Right-click on LATEST and open the same in a New-window and print title of  

 Actions action = new Actions(driver);

 WebElement element = driver.findElement(By.xpath("//a[text()='Latest']"));

  action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

Related Questions In Selenium

0 votes
1 answer

Click a link within an iFrame which opens a new tab and switch to it

While switching the TAB induce WebDriverWait and switch accordingly. Below code ...READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,890 points
1,866 views
0 votes
3 answers

How to click the search button using Selenium web driver and Python

You can try with tag. #this code will ...READ MORE

answered Apr 10, 2019 in Selenium by Matin
10,213 views
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,110 points
20,750 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
3,785 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,766 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,742 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,111 views
+1 vote
1 answer

What is Appium and how it uses Selenium Webdriver?

Hey Drejina, Appium is an open source ...READ MORE

answered Jul 7, 2019 in Selenium by Martin
979 views
0 votes
1 answer

What are the differences between Selenium and Quick Test Professional (QTP)?

Hello Siddhant, some of the major differences ...READ MORE

answered Jul 7, 2019 in Selenium by Pratibha
90 views