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
333 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("http://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?

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,870 points
796 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
5,033 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,010 points
7,415 views
0 votes
1 answer

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,510 points
1,582 views
0 votes
1 answer
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,870 points
1,505 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
1,294 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
96 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
49 views