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`sを続けますタブが開きます。それは同じタブでURLの `を開けておく..私はまた、アクションを使用してみました。セレンJavaの新しいタブが - 開くことはありませんが、私は新しいを得ることができない私は前のタブを開いたまま、新しいタブを開くしようとしています

+1

チェックこのそれは場合に役立ちます - http://qaperspective.blogspot.in/2016/09/open-new-tab-using-Selenium-WebDriver.html – Amol

+0

うんおかげで、この作品:((JavascriptExecutor)ドライバ).executeScript( "window.open( ''、 '_ blank');"); –

+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とSelenium 3.0.1を使用しています。 –

+0

これはうまくいき、あなたのスイッチは動作します:((JavascriptExecutor)ドライバ).executeScript( "window .open( ''、 '_ blank'); "); –

+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

ChromeでSelenium Javaを使用して新しいタブを開くには、これを試してください。

 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/');"); 

     } 
    } 
関連する問題