1
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);   
    } 
} 

我試圖打開一個新的標籤,離開原來標籤打開.. 我不能得到一個新的標籤打開。它保持開放URL在同一個標​​籤..我也嘗試使用操作。硒Java的新標籤在Chrome - 不會公開,保持在同一個選項卡打開URL`s

+1

檢查這是否有幫助 - http://qaperspective.blogspot.in/2016/09/open-new-tab-using-Selenium-WebDriver.html – Amol

+0

好的,謝謝,這工作:((JavascriptExecutor)驅動程序).executeScript( 「window.open( '', '_空白');」); –

+0

很好用,它可以在所有現代瀏覽器上運行,因爲它們支持開箱即用的javascript。對於無頭瀏覽器,你可以參考這個鏈接http://stackoverflow.com/questions/814757/headless-internet-browser/814929#814929 – Amol

回答

1

您需要的驅動程序切換到新的選項卡。在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); 

作爲一個側面說明,implicitlyWait是驅動程序,而不是標籤/窗口中定義。打開新選項卡後無需再次定義它。

+0

它仍然打開(在這種情況下gmail)在同一個標​​籤..我使用Chrome版本54.0.2840.71和硒3.0.1 .. –

+0

這工作,你的開關工作:((JavascriptExecutor)驅動程序).executeScript(「窗口。開( '', '_空白');「); –

+0

和chromedriver版本? – monami

0

您在駕駛前忘記將驅動程序的焦點更改爲新選項卡。

試試這個打開一個新標籤後:

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 
+0

我不認爲切換是問題,因爲新的選項卡從來沒有打開過.. driver.findElement(By.cssSelector(「body」)) ).sendKeys(Keys.CONTROL +「t」); - 沒有工作 –

+0

啊,好的。我將編輯我的答案,包括如何打開一個新標籤。 –

+0

解決它與JS:((JavascriptExecutor)驅動程序).executeScript(「window.open('','_ blank');」);但是如果我不能使用JS,有沒有辦法打開一個沒有JS的新選項卡? –

0

請嘗試使用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/');"); 

     } 
    } 
相關問題