> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/integrations/cn/proxy-integrations/residential-proxies-guides/selenium.md).

# Selenium

[**Selenium**](https://www.selenium.dev/) 是一个帮助自动化网页浏览器交互的工具，适用于网站测试等场景。

要将 Selenium 与 Oxylabs 集成 [**住宅代理**](https://oxylabs.io/products/residential-proxy-pool)，请按照以下说明操作，或观看此视频教程：

{% embed url="<https://www.youtube.com/watch?v=uh71blwf1l0>" %}

**步骤 1。** 安装 [**Selenium Wire**](https://github.com/wkeeling/selenium-wire) 来扩展 Selenium 的 Python 绑定，因为使用默认的 Selenium 模块来实现需要身份验证的代理会让整个过程变得复杂。你可以使用 `pip` 命令： `pip install selenium-wire`

另一个推荐用于此集成的包是 `webdriver-manager`。它是一个包，可简化不同浏览器二进制驱动的管理。在这种情况下， **每次更新后都无需手动下载新的 web driver 版本**.

你可以安装 `webdriver-manager` 使用 `pip` 命令： `pip install webdriver-manager`

**步骤 2。** 请指定你的账户凭据，以使代理正常工作：

`USERNAME = "your_username"`

`PASSWORD = "your_password"`

`ENDPOINT = "pr.oxylabs.io:7777"`

您需要调整`your_username` 并 `your_password` 包含您的代理用户的用户名和密码的字段。

{% hint style="info" %}
您可以在我们的 [**仪表板**](https://dashboard.oxylabs.io/en/)**.**
{% endhint %}

**步骤 3**。通过访问以下地址检查代理是否正常工作 [**ip.oxylabs.io**](https://ip.oxylabs.io/)。如果一切正常 - 它将返回您正在使用的代理的 IP 地址。

```
尝试：
    driver.get("https://ip.oxylabs.io/")
    return f'\n你的 IP 是：{driver.find_element(By.CSS_SELECTOR, "pre").text}'
finally:
    driver.quit()
```

Oxylabs 的完整代码 [**住宅代理**](https://oxylabs.io/products/residential-proxy-pool) 与 Selenium 的集成：

```
from selenium.webdriver.common.by import By
from seleniumwire import webdriver
# 一个始终保持 chromedriver 为最新版本的软件包。
from webdriver_manager.chrome import ChromeDriverManager

USERNAME = "your_username"
PASSWORD = "your_password"
ENDPOINT = "pr.oxylabs.io:7777"

def chrome_proxy(user: str, password: str, endpoint: str) -> dict:
    wire_options = {
        "proxy": {
            "http": f"http://{user}:{password}@{endpoint}",
            "https": f"https://{user}:{password}@{endpoint}",
        }
    }

    return wire_options

def execute_driver():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    代理 = chrome_proxy(USERNAME, PASSWORD, ENDPOINT)
    driver = webdriver.Chrome(
    ChromeDriverManager(driver_version='<VERSION>').install(), options=options, seleniumwire_options=代理
    )
    尝试：
        driver.get("https://ip.oxylabs.io/")
        return f'\n你的 IP 是：{driver.find_element(By.CSS_SELECTOR, "pre").text}'
    finally:
        driver.quit()


if __name__ == '__main__':
    print(execute_driver())
```

如果你想了解更多关于使用 Selenium 进行网页抓取的信息，请阅读我们的 [**博客文章**](https://oxylabs.io/blog/selenium-web-scraping).

查找原始的 Selenium 集成博客文章 [这里](https://oxylabs.io/resources/integrations/selenium).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.oxylabs.io/integrations/cn/proxy-integrations/residential-proxies-guides/selenium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
