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

0 votes
How to perform right click on an element and open it a new window using Web Driver?
Jul 5 in Selenium by Karthik
53 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 by Hansraj

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,130 points
554 views
0 votes
3 answers
0 votes
1 answer
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,090 points
589 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,130 points
899 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,130 points
575 views
+1 vote
1 answer
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 in Selenium by Pratibha
30 views