# Integrating Selenium with XProxy

> For AI tools: this is the Markdown copy of https://xproxy.io/document/integrating-selenium-with-xproxy. Every XProxy guide has one at its URL + `.md`; the index of all guides is https://xproxy.io/llms.txt.

Send Selenium and Chrome through an XProxy mobile proxy (Whitelist IP) and change the IP from Python.

Send Selenium and Chrome through an XProxy mobile proxy, and change the IP from your Python code.

In short

**Proxy in Chrome options**

One `--proxy-server` argument sends Chrome through your XProxy proxy.

**New IP from code**

Call one URL on your panel before you start Chrome. The modem gets a new IP in a few seconds.

**Easy to check**

Open an IP page and print it. It must match the **Public IP** of that proxy in your panel.

**You need:** Python 3 with Selenium and Chrome installed, and an XProxy panel with a working modem.

## Chrome needs a proxy without a password

Chrome cannot take a username and password in `--proxy-server`. Use the **Whitelist IP** login for this script:

1. In your XProxy panel open **Proxy Settings → Login** and turn on **Whitelist IP** (Basic plan or higher).
2. Add the public IP of the computer that runs Selenium.
3. In **Proxy Management**, click **Export Proxy**. Set **Protocol** to **HTTP** and **Credentials** to **No credentials** (`host:port`), then click **Copy All**.

Need a password instead? Use [Playwright](https://xproxy.io/document/integrating-puppeteer-playwright-with-xproxy): it takes a username and a password in its launch options. More about the two login methods: [Copy a proxy in the right format](https://xproxy.io/document/how-use-proxy-format-http-socks5).

## Open a page through XProxy

**Proxy**

```
from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://192.168.1.10:4001")  # from Export Proxy
options.add_argument("--headless=new")  # optional: no window

driver = webdriver.Chrome(options=options)
try:
    driver.get("https://api.ipify.org?format=json")
    print(driver.find_element(By.TAG_NAME, "body").text)  # {"ip":"..."}
finally:
    driver.quit()
```

Needs `pip install selenium`. Selenium 4.6 or newer finds the Chrome driver for you.

**New IP, then browse**

```
import time

import requests

PANEL = "http://192.168.1.10"      # your XProxy panel address
POSITION = 1                       # the Pos number in Proxy Management
PROXY_ADDR = "192.168.1.10:4001"   # host:port of the same modem

def rotate_ip(timeout=60):
    """Ask the modem for a new IP, then wait until it is back online."""
    r = requests.get(f"{PANEL}/api/v1/rotate_ip/position/{POSITION}", timeout=10)
    r.raise_for_status()
    if not r.json().get("status"):
        raise RuntimeError(f"Rotation refused: {r.text}")
    time.sleep(3)  # the modem reconnects in a few seconds
    deadline = time.time() + timeout
    while time.time() < deadline:
        s = requests.get(f"{PANEL}/status", params={"proxy": PROXY_ADDR}, timeout=10).json()
        if s.get("msg") == "MODEM_READY":
            return s.get("public_ip")
        time.sleep(1)
    raise TimeoutError("The modem did not come back in time")

from selenium import webdriver
from selenium.webdriver.common.by import By

print("New IP:", rotate_ip())  # change the IP before Chrome starts

options = webdriver.ChromeOptions()
options.add_argument(f"--proxy-server=http://{PROXY_ADDR}")
options.add_argument("--headless=new")  # optional: no window

driver = webdriver.Chrome(options=options)
try:
    driver.get("https://api.ipify.org?format=json")
    print(driver.find_element(By.TAG_NAME, "body").text)
finally:
    driver.quit()
```

Needs `pip install selenium requests`. Chrome reads the proxy once at start, so rotate before `webdriver.Chrome()` or start a new browser after you rotate.

## Change the IP from code

Position **1** is the first modem, the one on port **4001** when the starting port is 4000. The code calls the rotation API, then asks the panel for the modem's status until it says `MODEM_READY`. All calls are in [How to change / rotate IP](https://xproxy.io/document/how-change-rotate-ip).

**Panel asks for a login?** If **Force API Auth** is on, these calls need your dashboard login: add `auth=("USER", "PASS")` to each `requests.get`.

## Check it works

1. Run the first script. It prints an IP address.
2. In **Proxy Management**, look at the **Public IP** of that proxy (marked 1). It is the same IP.
3. Run the second script. It prints a new IP twice: the one from the panel call and the one Chrome sees.

![Proxy Management table with the Public IP of the first proxy marked (demo data)](https://media1.xproxy.io/docs/integrations/int-publicip-v1.webp)

The Public IP your script should print. Demo data.

## Something not working?

| You see | What to do |
| --- | --- |
| Chrome shows a proxy error page, or asks for a login | The proxy still wants a password. Add your computer's public IP to **Whitelist IP** as shown above, or use Playwright with a username and password. |
| Connection refused or timed out | The address is not reachable from your computer. A **LAN only** address works only on your own network: use **Public address** or **VPS relay** ([public access](https://xproxy.io/document/how-public-access-xproxy)). Also check the port in **Proxy Management**. |
| The printed IP is your own IP | The request did not go through the proxy. Check that the proxy is passed to the request, the browser or the crawler, and that it is not set only for `http` or only for `https`. |
| The rotation call fails or says `status` is false | Check the position number and the panel address. If **Force API Auth** is on, add your dashboard login to the call. |
| The IP is the same after rotating | Sometimes the carrier gives the modem the same IP. Rotate again. The status call can also answer `COLLISION_IP` when **IP unchanged detect** is on in Rotation Settings. |

## What next?

Change the IP on a timer, or copy your proxies in another format.

[Rotate IP: all ways →](https://xproxy.io/document/how-change-rotate-ip) [Proxy formats →](https://xproxy.io/document/how-use-proxy-format-http-socks5)

---

Source: https://xproxy.io/document/integrating-selenium-with-xproxy
