Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Ondřej Machulda edited this pageAug 30, 2022 ·9 revisions

This page covers specifics of using php-webdriver withFirefox browser andGeckodriver.

Contents

  1. Installation
  2. Start Geckodriver
  3. FirefoxOptions
  4. Firefox Profile

Installation

First you need to install the Firefox browser. However, this is not enough, as each supported browser also needs itsbrowser driver, in the case of Firefox it isGeckodriver (the name came from "Gecko" - browser engine used by Firefox). Browser driver is a binary file which enables the browser to be controlled using WebDriver protocol.

To use Geckodriver, you will need todownloadgeckodriver/geckodriver.exe executable. Make sure the path togeckodriver/geckodriver.exe is in your system PATH, so that you can easily start it. See officialSelenium documentation covering PATH setup.

ℹ️ Geckodriver may be available in some Linux distributions (for example asfirefox-geckodriver package in Ubuntu), so you can install the latest version using your system package manager.

⚠️ Make sure you have compatible version of Firefox and Geckodriver. This is most easily done by using latest stable version of Firefox and the latest released version of Geckodriver.

Start Geckodriver

As with other browsers, there are multiple ways how to start Geckodriver instance, each suitable for different scenario.

Start directly usingFirefoxDriver class

In this case php-webdriver takes care of starting and setting up geckodriver process. This is suitable for simple cases, when you run the browser locally on your computer.

If you installed geckodriver in your system PATH, you can start it like this without any additional steps:

$driver = FirefoxDriver::start();$driver->get('https://google.com');

When geckodriver binary is installed elsewhere, define path to the binary usingWEBDRIVER_FIREFOX_DRIVER environment variable:

putenv('WEBDRIVER_FIREFOX_DRIVER=/path/to/geckodriver');$driver = FirefoxDriver::start();

Or you can define the environment variable before starting the PHP script:

$export WEBDRIVER_FIREFOX_DRIVER=/path/to/geckodriver$ php my-test.php
// my-test.php$driver = FirefoxDriver::start();// ... your code// Don't forget to always call quit() at the end so that the geckodriver process is terminated$driver->quit();

Startgeckodriver binary manually

For this case, you start geckodriver instance by yourself. It must be kept running while you start your tests. This scenario is suitable for basic local development, and also when you need to adjust the way geckodriver is started (for example the port number) or when you want to define the browser usingDesiredCapabilities object.

$ geckodriver# or geckodriver.exe on Windows

Now start your PHP script in another terminal window:

// my-test.php$serverUrl ='http://localhost:4444';$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::firefox());
$ php my-test.php

Run test via Selenium server (grid) or in Docker

If you want to run your browser instances remotely (for example as a grid on CI server) or you want to start specificversion of browser/Selenium in Docker, seeSelenium server wiki page.

FirefoxOptions

For managing Firefox-specific capabilities, you can useFirefoxOptions class.

Full list ofavailable firefoxOptions could be found in Mozilla documentation.

See below for examples how to set and use FirefoxOptions in php-webdriver:

General usage

// Create an instance of FirefoxOptions:$firefoxOptions =newFirefoxOptions();// Configure $firefoxOptions, see examples bellow$firefoxOptions->addArguments([/*...*/]);$firefoxOptions->setPreference('example.preference','value');// Create $capabilities and add configuration from FirefoxOptions$capabilities = DesiredCapabilities::firefox();$capabilities->setCapability(FirefoxOptions::CAPABILITY,$firefoxOptions);// Start the browser with $capabilities// A) When using RemoteWebDriver::create()$driver = RemoteWebDriver::create($serverUrl,$capabilities);// B) When using FirefoxDriver::start to start local Geckodriver$driver = FirefoxDriver::start($capabilities);

Start Firefox in headless mode

$firefoxOptions->addArguments(['-headless']);

Set Firefox preferences (about:config)

Default value ofabout:config entries (Firefox preferences) could be modified usingprefs field:

$firefoxOptions->setPreference('javascript.options.showInConsole',false);$firefoxOptions->setPreference('dom.ipc.processCount',4);

More information about the preferences can by found inFirefox documentation.

Using a Firefox executable in a non-standard location

$firefoxOptions->setOption('binary','/home/user/Downloads/firefox-esr');

Firefox Profile

Another option how to adjust Firefox behavior is to use custom profile. This profile is transferred to the target machine where Firefox runs.

Custom Firefox Profile could be used for example to start Firefox with installed extensions, custom certificates etc.However, for setting custom preferences we recommend using thesetPreference() method onFirefoxOptions instead.

Create an instance of FirefoxProfile

$profile =newFirefoxProfile();

Add custom extension

$profile->addExtension('path/to/FirefoxExtension.xpi');

Start Firefox with the profile

$firefoxOptions =newFirefoxOptions();$firefoxOptions->setProfile($profile);$capabilities = DesiredCapabilities::firefox();$capabilities->setCapability(FirefoxOptions::CAPABILITY,$firefoxOptions);$driver = RemoteWebDriver::create('http://localhost:4444',$capabilities);

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp