Skip to content

Alert, tabs, frames, iframes

Ondřej Machulda edited this page Nov 28, 2020 · 8 revisions

The guide talks about how to switch between different alert, windows and frame.

Alert

For more details, read WebDriverAlert.

Waiting for the alert

$driver->wait()->until(
  WebDriverExpectedCondition::alertIsPresent(),
  'I am expecting an alert!'
);

Accepting an alert

// accepting the alert
$driver->switchTo()->alert()->accept();

Cancel an alert

// cancel/dismiss the alert
$driver->switchTo()->alert()->dismiss();

Get the message on the alert

// get the text on the alert
$message = $driver->switchTo()->alert()->getText();

Answering question on the alert

// send keys to the alert
$driver->switchTo()->alert()->sendKeys($name);

Window, Tab

When you click a link or a button, a new window, tab or popup might appear and you need to switch to that window to fill a form or grant the permission, etc....

<a href='/new_window.php' target='_blank'>

Each window has an unique window handle, a string for WebDriver to identify the window and switch between them.

Get the current window handle

$current_handle = $driver->getWindowHandle();

Get all window handles

// Get all window handles available to the current webdriver session.
// This will returns an array of string
$handles = $driver->getWindowHandles();

Switch to the new window

// Get current window handles first:
$windowHandlesBefore = $this->driver->getWindowHandles();

// Here do some event which opens new tab/window, like click to link etc.

// Then get list of the new window handles and find out which window is the new one.
// Do not use end($handles)! See note below.
$windowHandlesAfter = $this->driver->getWindowHandles();
$newWindowHandle = array_diff($windowHandlesAfter, $windowHandlesBefore);

// Now you know handle of the new window and can switch to it:
$driver->switchTo()->window(reset($newWindowHandle));

Note: Do not use end($handles) to find the last open window, because getWindowHandles() does not necessarily return window in the order in which they were opened.

Create a new tab or window

In W3C protocol mode (recommended)

// Default behavior, without specifying window type
$driver->switchTo()->newWindow();

// Try to open new window
$driver->switchTo()->newWindow(WebDriverTargetLocator::WINDOW_TYPE_WINDOW);
// Try to open new tab
$driver->switchTo()->newWindow(WebDriverTargetLocator::WINDOW_TYPE_TAB);

Note the browser may not open the desired window type (tab or window) if it doesn't support it, and may open different supported type instead.

Workaround in legacy OSS protocol mode

// using the browser shortcut to create a new tab
$driver->getKeyboard()->sendKeys(
  array(WebDriverKeys::CONTROL, 't'),
);

// using the browser shortcut to create a new window
$driver->getKeyboard()->sendKeys(
  array(WebDriverKeys::CONTROL, 'n'),
);

Frame, iframe

In order to interact with elements inside a frame or iframe, switch to that frame first!

Switching to another frame or iframe

$myFrame is a WebDriverElement, the iframe element found by $driver->findElement($by).

$my_frame = $driver->findElement(WebDriverBy::id('my_frame'));
$driver->switchTo()->frame($myFrame);

You can also identify frame/iframe by its identifier (number)"

$driver->switchTo()->frame(1);

Switching back to the main frame on the page

$driver->switchTo()->defaultContent();

Get the active element

Switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected. This matches the semantics of calling document.activeElement in Javascript.

$active_element = $driver->switchTo()->activeElement();

Reference

WebDriverTargetLocator manages the focus of WebDriver. Read the source code if you want to know more.

Clone this wiki locally