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
8,566 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,910 points
4,673 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
16,283 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,150 points
33,763 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,500 points
13,766 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,185 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,910 points
7,259 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 3, 2020 in Selenium by Sri
• 3,190 points
7,897 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
3,148 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
662 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP