Example command reference

Ondřej Machulda edited this page Apr 10, 2018 · 49 revisions

Example command reference


Here is my attempt at helping others learn to use this binding. Others out there who are more experienced with this PHP binding, please add to this page as you get to it please.

Working with elements

NOTE: some examples below may be done shorthand, in single line chained calls from the session object, though if we wanted to perform several actions on the same element, we could save the WebDriverElement into a variable and then make subsequent calls from that element.

Getting text of an element

$result = $driver->findElement(WebDriverBy::id('signin'))->getText();

Triggering MouseOver Events

$element = $driver->findElement(WebDriverBy::id('some_id'));
$driver->getMouse()->mouseMove( $element->getCoordinates() );

Clicking an element (link, checkbox, etc.)


Typing into text field

$driver->findElement(WebDriverBy::id("element id"))->sendKeys("I want to send this");

How to clear the existing value in text field

If a text field already has a non-empty value and you want to update that field, here's how to clear the existing value:

$driver->findElement(WebDriverBy::id("element id"))->clear();

This also works for editable divs.

How to check if an element is visible

$element = $driver->findElement(WebDriverBy::id('element id'));
if ($element->isDisplayed()) {
    // do something...

How to reload the page


The other option is to run a Javascript snippet (scroll down to see how to do this). More methods related to navigation can be found here.

Alerts, confirmations and prompts


$driver->switchTo()->alert()->dismiss(); // dismiss
$driver->switchTo()->alert()->getText(); // get alert text, works for confirmations and prompts


$driver->switchTo()->alert()->accept(); // accept
$driver->switchTo()->alert()->dismiss(); // dismiss


$driver->switchTo()->alert()->sendKeys('Test'); // enter text
$driver->switchTo()->alert()->accept(); // submit

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


Maximize the browser window

// $driver->manage()->window()->maximum(); // old
// Use instead

Executing JavaScript

Synchronous script execution

Remember that the script you run is implicitly inserted into an anonymous javascript function. That means if you want to access global variables, you have to use the full name of the variable, for example window.document

$sScriptResult = $session->executeScript('return window.document.location.hostname',array());

$sScriptResult now holds the value of the current document hostname

Asynchronous script execution

Make sure you tell the server how long it should wait before it gives up on your script and throws a timeout exception.

// wait at most 5 seconds before giving up with a timeout exception

Similar to synchronous script, the async script is wrapped in an anonymous function.

Simple example

$sResult = $session->executeAsyncScript('arguments[arguments.length-1]("done");', array());

$sResult == "done"

More complex example

In the example below, we poll the global window.MY_STUFF_DONE value at regular intervals, waiting for it to exist with a non-false value. Once we see it, we return back to the calling php-webdriver code with the value "done".

// define the javascript code to execute. This just checks at a periodic
// interval to see if your page created the window.MY_STUFF_DONE variable
$sJavascript = <<<END_JAVASCRIPT

var callback = arguments[arguments.length-1], // webdriver async script callback
    nIntervalId; // setInterval id to stop polling

function checkDone() {
  if( window.MY_STUFF_DONE ) {
    window.clearInterval(nIntervalId); // stop polling
    callback("done"); // return "done" to PHP code

nIntervalId = window.setInterval( checkDone, 50 ); // start polling

$sResult = $session->executeAsyncScript($sJavascript,array());

$sResult == "done"

Window handling

Getting current window handle

$handle = $session->getWindowHandle();

Getting all window handles as array

$handles = $session->getWindowHandles(); //now can iterate through array to get desired handle

Switching to window


Switch Frame

//Find an iframe
$iframe = $session->findElement(WebDriverBy::tagName('iframe'));

//Get the iframe id
$frameId = $iframe->getAttribute('id');

//Switch the driver to the iframe

<Do whatever you needed to do in the i-frame (click a link, for me)>

//Go back to the main document

Get attribute of an element

$title = $driver->findElement(WebDriverBy::id('signin'))->getAttribute('title');

Get value of an input element

$inputElement = $driver->findElement(WebDriverBy::id('username'))
$value = $inputElement->getAttribute('value');

Take a screenshot

$filename = __DIR__ . '/myfile.png';
$screenshot = $driver->takeScreenshot();
file_put_contents($filename, $screenshot);