# Selenium

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

要将 Selenium 与 Oxylabs 集成 [**移动代理**](broken://pages/7c50b210d807de54753cf5eb3a868b41c8c528dd)，请按照以下说明操作：

**步骤 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 的完整代码 [**移动代理**](broken://pages/7c50b210d807de54753cf5eb3a868b41c8c528dd) 与 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}",
        }
    }

    返回 wire_options

定义 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: 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/mobile-proxies-guides/selenium-1.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.
