我使用下面的代码打开新的浏览器窗口,但它在同一个标签页面中打开链接:-(。我想打开新的浏览器窗口,清除所有Cookie而不关闭第一个窗口。
` Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("N").build().perform();
driver.get("https://www.facebook.com");`
我也尝试过这段代码,但它确实帮助了我:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_N);
driver.get("https://www.facebook.com");
任何帮助都会被暗示!!
答案 0 :(得分:0)
要启动浏览器的新实例,请实现以下代码示例:
// Store the current window url
String url = driver.getCurrentUrl();
// Create a new instance to open a new window
WebDriver driver2 = new FirefoxDriver(); // Use your own browser driver that you are using
// Go to the intended page [i.e, foo or some other link]
driver2.navigate().to(foo);
// Continue your code here in the new window...
// Close the popup window now
driver2.quit();
// No need to switch back to the main window; driver is still valid.
// demonstrate that the initial driver is still valid.
url = driver.getCurrentUrl();
答案 1 :(得分:0)
您可以使用以下java脚本打开新窗口:
((JavascriptExecutor)driver).executeScript("window.open(arguments[0])", "URL to open");
答案 2 :(得分:0)
第一件事是您打开新窗口的组合键不正确。正确的组合键应为 Control + N
打开新窗口时,您需要聚焦到该新窗口
您可以使用下面的代码段打开一个新的浏览器窗口并导航到URL。这是使用Robot
包中的java.awt.Robot
类。
public void openNewWindow(String url) throws AWTException {
// Initialize the robot class object
Robot robot = new Robot();
// Press and hold Control and N keys
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_N);
// Release Control and N keys
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_N);
// Set focus to the newly opened browser window
ArrayList <String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
// Continue your actions in the new browser window
driver.get(url);
}