Reputation: 495
How to open a new tab using Selenium WebDriver?
I want to open multiple links in new tabs. This is to achieve to finish off the build validation tasks as soon as possible. So, that in every new tab all smoke test related links could be opened and then within each tab which corresponds to a smoke test requirement, we can carry out the sanity test.
Upvotes: 15
Views: 70051
Reputation: 595
In the first solution, add a Thread.sleep(5000) statement. I had error Index out of bounds multiple times, until i added the sleep statement.
WebDriver wd = new FirefoxDriver();
wd.get("http://www.gmail.com");
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wd.manage().window().maximize();
//To open a new tab
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
try {Thread.sleep(5000);} catch (InterruptedException e) {}
//To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(wd.getWindowHandles());
wd.switchTo().window(tabs.get(1));
//To navigate to new link/URL in 2nd new tab
wd.get("http://facebook.com");
Upvotes: 0
Reputation: 350
Code:
WebDriver wd = new FirefoxDriver();
wd.get("http://www.gmail.com");
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wd.manage().window().maximize();
//To open a new tab
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
//To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(wd.getWindowHandles());
wd.switchTo().window(tabs.get(1));
//To navigate to new link/URL in 2nd new tab
wd.get("http://facebook.com");
Upvotes: 9
Reputation: 11
// To open a new tab to establish second player connection
((JavascriptExecutor)driver).executeScript("window.open('about:blank', '-blank')");
// To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
// To navigate to new link/URL in 2nd new tab
driver.get("http://localhost:8080");
I did this and it worked perfect!
Upvotes: 0
Reputation: 1387
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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.AWTException;
public class Tabs {
WebDriver driver;
Robot rb;
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://qaautomated.com");
}
@Test
public void openTab() {
//Open tab 2 using CTRL + t keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Open URL In 2nd tab.
driver.get("http://www.qaautomated.com/p/contact.html");
//Call switchToTab() method to switch to 1st tab
switchToTab();
//perform required actions on tab 1.
driver.findElement(By.xpath("//input[@id='6']")).click();
driver.findElement(By.xpath("//input[@id='plus']"));
driver.findElement(By.xpath("//input[@id='3']"));
driver.findElement(By.xpath("//input[@id='equals']"));
//Call switchToTab() method to switch to 2nd tab.
switchToTab();
//Call switchToTab() method to switch to 1st tab
switchToTab();
}
public void switchToTab() {
//Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
//Switch to current selected tab's content.
driver.switchTo().defaultContent();
}
@AfterTest
public void closeTabs() throws AWTException {
//Used Robot class to perform ALT + SPACE + 'c' keypress event.
rb =new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
} }
This example is taken from THIS BLOG POST
Upvotes: 1
Reputation: 344
The above solutions did not work for me. Not sure why, but the driver would navigate to the new url, but a new tab just won't open.
I finally managed to open new tab using this code:
IWebDriver driver = new ChromeDriver("path for chromedriver.exe");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
driver.Navigate().GoToUrl("url1");
js.ExecuteScript("window.open()");
driver.SwitchTo().Window(driver.WindowHandles[driver.WindowHandles.Count - 1]);
driver.Navigate().GoToUrl("url2");
driver.WindowHandles.Count - 1
will get you the last opened tab i.e. the new tab in this scenario. Hope this helps someone out.
Upvotes: -1
Reputation: 8597
The only way to open links in new tabs is by simulating key-board shortcuts. The following hold true in FFX, Chrome & IE
Selenium doesn't (currently) have any concept of tabs within a browser window, so in order to open the tab and then test it you HAVE to use option 3.
The following code will execute option 3. and then immediately close that new tab. (In C#)
new Actions(WebDriver)
.KeyDown(Keys.Control)
.KeyDown(Keys.Shift)
.Click(tab)
.KeyUp(Keys.Shift)
.KeyUp(Keys.Control)
.Perform();
new Actions(WebDriver)
.SendKeys(Keys.Control + "w")
.Perform();
You could also use:
.MoveToElement(tab)
.Click()
in the middle of the first option, and
.KeyDown(Keys.Control)
.KeyDown("w")
.KeyUp("w")
.KeyUp(Keys.Control)
in the second one.
Upvotes: 5
Reputation: 137
/* Open new tab in browser */
public void openNewTab()
{
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
}
Upvotes: 2
Reputation: 3129
We can use the Actions class of WebDriver. See following code:
WebDriver driver = new FirefoxDriver();
driver.get("<provide URL>");
WebElement link = driver.findElement(locator);
Actions builder = new Actions(driver);
Action openLinkInNewTab = builder
.moveToElement(link)
.sendKeys(link, Keys.CONTROL)
.click(link)
.keyUp(Keys.CONTROL)
.build();
openLinkInNewTab.perform();
This can be looped for multiple links.
Upvotes: 1