Selenium との連携

Learn how to route Python Selenium and Chrome Driver web automation through XProxy 4G/5G mobile proxies with automated IP rotation triggers.

Why use XProxy with Selenium?

Selenium is the industry standard framework for web scraping, browser automation, and bot development. Routing Selenium scrapers through XProxy self-hosted 4G/5G mobile proxies allows you to bypass Cloudflare anti-bot checks, IP rate limits, and CAPTCHAs by rotating real mobile carrier IPs on demand.

事前準備

  • Python 3.8+ with selenium package installed (pip install selenium requests).
  • XProxy server running with active 4G/5G USB dongles or Android phones.
  • Proxy SOCKS5 or HTTP credentials and Rotation API Link from XProxy Dashboard.

Python Selenium Code Example

Below is a production-ready Python script configuring Selenium Chrome Driver with XProxy HTTP/SOCKS5 proxy and triggering IP rotation before starting a scraping task:

selenium_xproxy_example.py
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# --- XProxy 設定手順 ---
XPROXY_HOST = "192.168.6.7"        # Your XProxy server IP
XPROXY_PORT = 5001                 # Port assigned to dongle #1 (HTTP/SOCKS5)
ROTATION_API_URL = "http://192.168.6.7:8081/api/v1/rotate_ip/position/1"

def rotate_xproxy_ip():
    """Trigger 4G/5G cellular reconnect on XProxy modem."""
    print("🔄 Requesting XProxy IP rotation...")
    try:
        res = requests.get(ROTATION_API_URL, timeout=10)
        if res.status_code == 200:
            print("✅ IP rotation triggered successfully. Waiting 5s for cellular reconnect...")
            time.sleep(5)
            return True
        else:
            print(f"⚠️ Failed to rotate IP: HTTP {res.status_code}")
    except Exception as e:
        print(f"❌ Error calling XProxy Rotation API: {e}")
    return False

def run_selenium_scraper():
    # 1. Rotate IP before launch
    rotate_xproxy_ip()

    # 2. Configure Chrome Driver Proxy Options
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server=http://{XPROXY_HOST}:{XPROXY_PORT}')
    chrome_options.add_argument('--headless')  # Run headless if desired
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome(options=chrome_options)

    try:
        # 3. Verify exit IP address
        print("🌐 Checking exit IP via Selenium...")
        driver.get("https://api.ipify.org?format=json")
        print("Response body:", driver.page_source)

        # 4. Perform your scraping tasks...
        driver.get("https://httpbin.org/ip")
        print("Page Title:", driver.title)
    finally:
        driver.quit()

if __name__ == "__main__":
    run_selenium_scraper()

Handling Authenticated Proxies in Selenium

If authentication is enabled on your XProxy channel (Username & Password), use selenium-wire or a lightweight extension like proxy-chrome-extension to supply credentials seamlessly:

selenium_wire_auth.py
from seleniumwire import webdriver

seleniumwire_options = {
    'proxy': {
        'http': 'http://username:[email protected]:5001',
        'https': 'https://username:[email protected]:5001',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

driver = webdriver.Chrome(seleniumwire_options=seleniumwire_options)
driver.get("https://httpbin.org/ip")
print(driver.page_source)
driver.quit()