import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenandClosenewtab {
public static void main(String[] a) throws InterruptedException {
// Initialize driver
WebDriver dr = new FirefoxDriver();
//Maximize browser window
dr.manage().window().maximize();
//Go to URL
dr.get("http://www.google.com");
//Set timeout
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Open new tab
dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
//Go to URL
dr.get("http://www.gmail.com");
//Set new tab timeout
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Do some operation
dr.findElement(By.id("gmail-sign-in")).click();
dr.findElement(By.id("Email")).sendKeys("WebDriver");
dr.findElement(By.id("Passwd")).sendKeys("WebDriver");
dr.findElement(By.id("signIn")).click();
Thread.sleep(2000);
// Close new tab
dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
// Switch first tab
dr.switchTo().defaultContent();
Thread.sleep(2000);
// Write search String
dr.findElement(By.id("gbqfq")).sendKeys("WebDriver");
// Click on Search button
dr.findElement(By.id("gbqfb")).click();
Thread.sleep(2000);
// Browser close
dr.close();
}
}
How to open and close tab in selenium webdriver using java?.
Subscribe to:
Post Comments (Atom)
dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); this line of code is not working in chrome browser . It simply opens in the same tab .
ReplyDeletebharath
ReplyDeleteShortcut key depend on Browser. Different Browser shortcut key is different
Works perfectly :) Thanks a lot.
ReplyDeletedr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); this line of code is not working in chrome browser . It simply opens in the same tab .
ReplyDeletedr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w"); is also not working
ReplyDeleteWith EasyHits4U you can earn free advertising credits by visiting other website-ads from a account base of over 1.2 million accounts. Earn advertising credits faster with a view per view model.
ReplyDelete