Skip to content

HowTo Work with proxy

Ondřej Machulda edited this page Apr 26, 2023 · 12 revisions

Setting a proxy for your browser

Example for an HTTP and SSL proxy:

$seleniumServerUrl = 'http://localhost:4444';
$proxyUrl = '127.0.0.1:2043';

$capabilities = new DesiredCapabilities(
    [
        WebDriverCapabilityType::BROWSER_NAME => 'firefox',
        WebDriverCapabilityType::PROXY => [
            'proxyType' => 'manual',
            'httpProxy' => $proxyUrl,
            'sslProxy' => $proxyUrl,
            'noProxy' => ['google.com'],
    ],
]);
$driver = RemoteWebDriver::create($seleniumServerUrl, $capabilities);

See W3C WebDriver specification for more proxy configuration options.

Proxy with authentication

Using proxy with credentials is currently not implemented in neither Chrome or Firefox, see related issues:

As a workaround, it is recommended to put you own local proxy (for example mitmproxy) in front of the remote authenticated proxy. You will then pass URL of the local proxy (eg. localhost:8080) as part of capabilities (see example above), and configure this custom proxy to forward requests to the remote proxy with authentication.

For example to start local mitmproxy which will forward requests to remote proxy running on https://hostname:port with credentials username:password use:

mitmproxy --mode upstream:https://hostname:port --upstream-auth username:password

This will start proxy on localhost:8080 (use $proxyUrl = 'localhost:8080' as per example above).