Tuesday, 26 August 2014

Right Click and Open a new tab in Selenium Web Driver



To right click and open a new tab in Selenium WebDriver.

   1. Write a simple code to launch firefox browser and open a URL like yahoo in it.

WebDriver driver=new FirefoxDriver();
driver.get("https://in.yahoo.com/?p=us");
                  
driver.manage().window().maximize();

   2. Now to open the new link in another tab. Write the below code.

WebElement oWE=driver.findElement(By.linkText("Mail"));

   3. To right click the link and open it in a new tab.

   Actions oAction=new Actions(driver);
  oAction.moveToElement(oWE);
                    oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

   4. This will open a new tab for link mail in yahoo. Please find entire source code for opening a new tab using right click.

  package TestPackage;

import java.io.File;
import java.io.IOException;

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 test1 {
           
                public static void main(String[] args) throws IOException
                {
                    WebDriver driver=new FirefoxDriver();
                    driver.get("https://in.yahoo.com/?p=us");
                  
                    driver.manage().window().maximize();
                  
                    WebElement oWE=driver.findElement(By.linkText("Mail"));
                  
                    Actions oAction=new Actions(driver);
                    oAction.moveToElement(oWE);
                    oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
}


8 comments: