package javaapplication1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaApplication1 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Aca\\desktop\\chromedriver.exe");
// Initialize driver
WebDriver driver = new ChromeDriver();
//Maximize browser window
driver.manage().window().maximize();
//Go to URL
driver.get("http://www.google.com");
//Set timeout
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Open new tab – May be you are stuck here
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
//Go to URL
driver.get("http://www.gmail.com");
//Set new tab timeout
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
I am trying to open a new tab, leaving the previous tab opened .. I can't get a new tab opened. It keeps opening URL`s in the same tab.. I also tried using Actions.
我正在尝试打开一个新选项卡,打开上一个选项卡..我无法打开新选项卡。它在同一个标签中保持打开URL。我也尝试使用Actions。
4 个解决方案
#1
1
You need to switch the driver to the new tab. In Chrome its done like switching windows
您需要将驱动程序切换到新选项卡。在Chrome中,它就像切换窗口一样
String tabHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(tabHandle))
{
driver.switchTo().window(handle);
}
}
driver.get("http://www.gmail.com");
// close the new tab and switch back to the old one
driver.close();
driver.switchTo().window(tabHandle);
As a side note, implicitlyWait
is defined for the driver, not tab/window. No need to define it again after opening the new tab.
作为旁注,隐含的是为驱动程序定义的,而不是选项卡/窗口。打开新选项卡后无需再次定义它。
#2
1
This is an issue with chromedriver itself. See the related bug submitted
这是chromedriver本身的一个问题。查看提交的相关错误
#3
0
You are forgetting to change the focus of the driver to the new tab before navigating.
在导航之前,您忘记将驱动程序的焦点更改为新选项卡。
Try this after opening a new tab:
打开新标签后尝试此操作:
driver.sendKeys(Keys.chord(Keys.CONTROL,"t"));
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); //Get tabs
driver.switchTo().window(tabs.get(1)); //Navigate to new tab
driver.get("http://www.gmail.com"); //Navigate within new tab
driver.close(); //Close the tab when done
driver.switchTo().window(tabs.get(0)); //Navigate to original tab
#4
0
Please try this to open new tab in Chrome with Selenium Java.
请尝试使用Selenium Java在Chrome中打开新标签页。
package com.sample;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class NewWindow {
public static void main (String[] args){
System.setProperty ("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-arguments");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
}
}
#1
1
You need to switch the driver to the new tab. In Chrome its done like switching windows
您需要将驱动程序切换到新选项卡。在Chrome中,它就像切换窗口一样
String tabHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(tabHandle))
{
driver.switchTo().window(handle);
}
}
driver.get("http://www.gmail.com");
// close the new tab and switch back to the old one
driver.close();
driver.switchTo().window(tabHandle);
As a side note, implicitlyWait
is defined for the driver, not tab/window. No need to define it again after opening the new tab.
作为旁注,隐含的是为驱动程序定义的,而不是选项卡/窗口。打开新选项卡后无需再次定义它。
#2
1
This is an issue with chromedriver itself. See the related bug submitted
这是chromedriver本身的一个问题。查看提交的相关错误
#3
0
You are forgetting to change the focus of the driver to the new tab before navigating.
在导航之前,您忘记将驱动程序的焦点更改为新选项卡。
Try this after opening a new tab:
打开新标签后尝试此操作:
driver.sendKeys(Keys.chord(Keys.CONTROL,"t"));
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); //Get tabs
driver.switchTo().window(tabs.get(1)); //Navigate to new tab
driver.get("http://www.gmail.com"); //Navigate within new tab
driver.close(); //Close the tab when done
driver.switchTo().window(tabs.get(0)); //Navigate to original tab
#4
0
Please try this to open new tab in Chrome with Selenium Java.
请尝试使用Selenium Java在Chrome中打开新标签页。
package com.sample;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class NewWindow {
public static void main (String[] args){
System.setProperty ("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-arguments");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
}
}