2017-05-27 6 views
1

同じブラウザで新しいタブを開こうとしていますが、動作しないようです。私はChromeバージョン58.0.3029.110(64ビット)とSelenium 3.0.0を使用しています。Selenium:Chromeの同じブラウザで新しいタブを開くことができません

私は以下のコードを使用:以下のようにJavascriptExecutorを使用してみてください

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");

+0

からスニペット? –

答えて

2

を:

((JavascriptExecutor) driver).executeScript("window.open('https://www.google.com');"); 
+0

これがうまくいった、ありがとう – Joe

0

ます。また、新しいタブを開くには、セレンwebdriverをして​​Robotクラスを使用することができます。

  1. Robotクラスを使用してキーボードのCtrl + tキーを押すことをシミュレートします。
  2. driver.switchTo()コマンドを使用して、セレンの新しいタブに切り替えます。
  3. 新しいタブで目的のリンクを開きます。

コードsnippet-

//Launch the first URL 
driver.get("http://www.google.com"); 

//Use robot class to press Ctrl+t keys   
Robot robot = new Robot();       
robot.keyPress(KeyEvent.VK_CONTROL); 
robot.keyPress(KeyEvent.VK_T); 
robot.keyRelease(KeyEvent.VK_CONTROL); 
robot.keyRelease(KeyEvent.VK_T); 

//Switch focus to new tab 
ArrayList<String> tabs = new ArrayList<String (driver.getWindowHandles()); 
driver.switchTo().window(tabs.get(1)); 

//Launch URL in the new tab 
driver.get("http://google.com"); 

出典:uはクロームの設定を検索しましたコードはOpen a new tab in Selenium - ArtOfTesting

関連する問題