Monday, May 12, 2014

How to open a link in new tab.



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class NewTab {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.id("gb_70"));
        act.moveToElement(link).contextClick().sendKeys("T").perform();
    }
}

3 comments:

  1. Hi dude, if you don't mind Can you please explain about this statement. Why contextClick() and sendKeys("T")?

    act.moveToElement(link).contextClick().sendKeys("T").perform();

    ReplyDelete
  2. contextClick() is used to right click and sendKeys("T") is used to send 'T'.
    Try manually right click on any link and then press T to open that link in new tab.

    ReplyDelete