LoginSignup
1
2

More than 3 years have passed since last update.

PHPのSeleniumでタブの切り替えを行う方法

Last updated at Posted at 2019-04-03

PHPのSeleniumで2つのタブの切り替えを行う方法。

$url = "https://www.google.com/"
$main = $driver->getWindowHandle();
$sub = null;

$driver->executeScript("window.open(\". $url . "\", null, null)");

foreach ( $driver->getWindowHandles() as $handle ) { 
  if ( $handle != $main ) {
    $sub = $handle;
  }
}

$driver->switchTo()->window($sub);

/* やりたい処理 */

$driver->close();

$driver->switchTo()->window($main);

自分のためのめも

  • $driver->executeScript( ~~ );で新しいタブを開く
  • foreach ( ~~ )では全ウィンドウハンドルを取得して、新しいタブのウィンドウハンドルを$subに格納
  • $driver->switchTo()->window($sub);$sub(新しいタブ)に切り替え
  • $driver->close();で新しいタブを閉じる
  • $driver->switchTo()->window($main);で元のタブに切り替える
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2