WebDriver driver = new FirefoxDriver(); // Firefox or any other Driver
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open()");
打开一个新标签后,它需要切换到该标签:
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
/* 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));
}
Dim driver = New WebDriver("Firefox", BaseUrl)
' Open new tab - send Control T
Dim body As IWebElement = driver.FindElement(By.TagName("body"))
body.SendKeys(Keys.Control + "t")
' Go to a URL in that tab
driver.GoToUrl("YourURL")
' Assuming you have m tabs open, go to tab n by sending Control + n
body.SendKeys(Keys.Control + n.ToString())
' Now set the visible tab as the drivers default content.
driver.SwitchTo().DefaultContent()
// Open a new tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
// URL to open in a new tab
String urlToOpen = "https://url_to_open_in_a_new_tab";
Iterator<String> windowIterator = driver.getWindowHandles()
.iterator();
// I always get handlesSize == 1, regardless how many tabs I have
int handlesSize = driver.getWindowHandles().size();
// I had to grab the original handle
String originalHandle = driver.getWindowHandle();
driver.navigate().to(urlToOpen);
Actions action = new Actions(driver);
// Close the newly opened tab
action.keyDown(Keys.CONTROL).sendKeys("w").perform();
// Switch back to original
action.keyDown(Keys.CONTROL).sendKeys("1").perform();
// And switch back to the original handle. I am not sure why, but
// it just did not work without this, like it has lost the focus
driver.switchTo().window(originalHandle);
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 the first tab
switchToTab();
// Call switchToTab() method to switch to the second 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);
}
}
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()) // Switch to new opened window
{
driver.switchTo().window(winHandle);
}
driver.switchTo().window(winHandleBefore); // Move to previously opened window
// The script that will will open a new blank window
// If you want to open a link new tab, replace 'about:blank' with a link
String a = "window.open('about:blank','_blank');";
((JavascriptExecutor)driver).executeScript(a);
package test;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Tab_Handle {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "geckodriver_path");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
// Store all currently open tabs in Available_tabs
ArrayList<String> Available_tabs = new ArrayList<String>(driver.getWindowHandles());
// Click on link to open in new tab
driver.findElement(By.id("Url_Link")).click();
// Switch newly open Tab
driver.switchTo().window(Available_tabs.get(1));
// Perform some operation on Newly open tab
// Close newly open tab after performing some operations.
driver.close();
// Switch to old(Parent) tab.
driver.switchTo().window(Available_tabs.get(0));
}
}
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.window import WindowTypes
driver.switch_to.new_window(WindowTypes.TAB)
打开新窗口
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.window import WindowTypes
driver.switch_to.new_window(WindowTypes.WINDOW)
爪哇咖啡
打开新窗口
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Opens LambdaTest homepage in the newly opened window
driver.navigate().to("https://www.lambdatest.com/");
打开新标签
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.TAB);
// Opens LambdaTest homepage in the newly opened tab
driver.navigate().to("https://www.lambdatest.com/")
public void openLinkInNewTab(String link){
String currentHandle = driver().getWindowHandle();
((JavascriptExecutor) driver()).executeScript("window.open()");
//getting all the handles currently available
Set<String> handles = driver().getWindowHandles();
for (String actual : handles) {
if (!actual.equalsIgnoreCase(currentHandle)) {
//switching to the opened tab
driver().switchTo().window(actual);
//opening the URL saved.
driver.get(link);
}
}
}
driver = new ChromeDriver(options);
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(@href,'facebook.com')])[1]")).getAttribute("href");
/*Create an another instance of driver.*/
driver = new ChromeDriver(options);
driver.get(facebookUrl);