> [!WARNING]
> [Zyte API](../../zyte-api/get-started.md#zyte-api) is replacing Smart Proxy Manager. It is
> no longer possible to sign up to Smart Proxy Manager. If you are an
> existing Smart Proxy Manager user, see spm-migrate.

# Using Smart Proxy Manager with Selenium

> [!WARNING]
> For the code below to work you must first [install the Zyte
> CA certificate](../../misc/ca.md#ca).

> [!NOTE]
> Selenium requires [language-specific libraries](https://www.selenium.dev/documentation/en/selenium_installation/installing_selenium_libraries/), [WebDriver](https://www.selenium.dev/documentation/en/selenium_installation/installing_webdriver_binaries/) installed and an active Smart Proxy Manager account. [Log in here](https://app.zyte.com/account/login).

> [!NOTE]
> All the code in this documentation has been tested with Geckodriver 0.30.0, Python 3.9.5 (Selenium 4.0.0), NodeJS v14.15.1 (selenium-webdriver 4.1.0) and Java 17.

## Option 1 - Zyte SmartProxy Selenium

1. Install [ChromeDriver](https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/#2-the-path-environment-variable) for Chrome.
2. Download and install [Zyte SmartProxy Selenium](https://github.com/zytedata/zyte-smartproxy-selenium):
   ```
   $ python3 -m pip install zyte-smartproxy-selenium
   ```
3. Save the following code as a `sample.py`:

```python
from zyte_smartproxy_selenium import webdriver

browser = webdriver.Chrome(spm_options={'spm_apikey': '<Smart Proxy Manager API KEY>'})
browser.get('https://toscrape.com')
browser.save_screenshot('screenshot.png')
browser.close()
```

1. Run `sample.py` with:
   ```
   $ python3 sample.py
   ```

## Option 2 - Zyte SmartProxy Headless Proxy

1. Setup the Zyte SmartProxy (formerly Crawlera) Headless Proxy as described in spm-headless.
2. Install [geckodriver](https://github.com/mozilla/geckodriver/releases) for Firefox.

### Python

Save the following code as a `sample.py` and run using `python sample.py`:

```python
from selenium import webdriver

HEADLESS_PROXY = "localhost:3128"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy": HEADLESS_PROXY,
    "sslProxy": HEADLESS_PROXY,
    "proxyType": "MANUAL",
}

with webdriver.Firefox() as driver:
    driver.get("https://toscrape.com")
    driver.save_screenshot("screenshot.png")
```

### Javascript

Save the following code as a `sample.js` and run using `node sample.js`:

```javascript
let webdriver = require('selenium-webdriver');
let firefox = require('selenium-webdriver/firefox');
let proxy = require('selenium-webdriver/proxy');
const fs = require('fs').promises
let opts = new firefox.Options();

(async function example() {
    opts.setProxy(proxy.manual({http: 'localhost:3128'}));
    let driver = new webdriver.Builder()
        .forBrowser('firefox')
        .setFirefoxOptions(opts)
        .build();
    try {
        await driver.get("https://toscrape.com");
        let image = await driver.takeScreenshot()
        await fs.writeFile('screenshot.png', image, 'base64');
    }
    finally {
        await driver.quit();
    }
}());
```

### Java

A sample Java class:

```java
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class SPMTest {

    public static void main(String[] args) throws InterruptedException, IOException {

        String headlessProxyHost = "localhost:3128";

        FirefoxOptions options = new FirefoxOptions();
        options.addArguments("--ignore-certificate-errors");

        Proxy proxy = new Proxy();
        proxy.setHttpProxy(headlessProxyHost);
        proxy.setSslProxy(headlessProxyHost);
        options.setCapability("proxy", proxy);

        WebDriver browser = new FirefoxDriver(options);

        String url = "https://toscrape.com";

        browser.get(url);
        System.out.println(browser.getPageSource());
        File image = ((TakesScreenshot) browser).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(image, new File("screenshot.png"));
        browser.quit();
    }
}
```
