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