title | linkTitle | weight | description | aliases | |||
---|---|---|---|---|---|---|---|
安装浏览器驱动 |
安装驱动 |
4 |
设置您的浏览器用于自动化.
|
|
通过WebDriver, Selenium支持市场上所有主要浏览器, 如Chrome、Firefox、Internet Explorer、Edge和Safari. WebDriver尽量使用浏览器内置的自动化支持 来驱动浏览器.
由于除Internet Explorer之外的所有驱动程序实现 都是由浏览器供应商自己提供的, 因此标准Selenium发行版中不包括这些驱动程序. 本节介绍了使用不同浏览器的基本要求.
在我们的[驱动程序配置]({{< ref "/documentation/webdriver/drivers/" >}}) 文档中 阅读有关启动驱动程序的更多高级选项.
{{% pageinfo color="warning" %}}
Page being translated from English to Chinese. Do you speak Chinese? Help us to translate it by sending us pull requests!
{{% /pageinfo %}}{{< badge-version version="4.6" >}}
Selenium Manager helps you to get a working environment to run Selenium out of the box. Beta 1
of Selenium Manager will configure the drivers for Chrome, Firefox, and Edge if they are not
found on the PATH
. No extra configuration is needed. Future releases of Selenium Manager
will eventually even download browsers if necessary.
Read more at the blog announcement for Selenium Manager .
大多数机器会自动更新浏览器, 但驱动程序不会. 为了确保为浏览器提供正确的驱动程序, 这里有许多第三方库可为您提供帮助.
{{< tabpane code=false langEqualsHeader=true >}} {{% tab header="Java" %}} Important: This package does not currently work for IEDriverServer v4+
import io.github.bonigarcia.wdm.WebDriverManager;
- 调用
setup()
会自动将正确的浏览器驱动程序 放在代码可以看到的位置:
WebDriverManager.chromedriver().setup();
- 只需像平常一样初始化驱动程序:
ChromeDriver driver = new ChromeDriver();
{{% /tab %}} {{% tab header="Python" %}}
from webdriver_manager.chrome import ChromeDriverManager
- 使用
install()
获取管理器使用的位置, 并将其传递到服务类中
service = Service(executable_path=ChromeDriverManager().install())
- 使用
Service
实例并初始化驱动程序:
driver = webdriver.Chrome(service=service)
{{% /tab %}} {{% tab header="CSharp" %}}
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
- 使用
SetUpDriver()
时需要一个配置类:
new DriverManager().SetUpDriver(new ChromeConfig());
- 像往常一样初始化驱动程序:
var driver = new ChromeDriver()
{{% /tab %}} {{% tab header="Ruby" %}}
- 增加 webdrivers gem 到 Gemfile:
gem 'webdrivers', '~> 5.0'
- 在程序中Require webdrivers:
require 'webdrivers'
- 像往常一样初始化驱动程序:
driver = Selenium::WebDriver.for :chrome
{{% /tab %}} {{% tab header="JavaScript" %}} There is not a recommended driver manager for JavaScript at this time {{% /tab %}} {{% tab header="Kotlin" %}}
import io.github.bonigarcia.wdm.WebDriverManager;
- 在初始化驱动程序之前调用setup方法:
fun chrome(): WebDriver {
WebDriverManager.chromedriver().setup()
return ChromeDriver()
}
{{% /tab %}} {{< /tabpane >}}
此选项首先需要手动下载驱动程序 (有关链接, 请参阅快速参考 部分).
这是一个灵活的选项, 可以在不更新代码的情况下更改驱动程序的位置, 并且可以在多台机器上工作, 而不需要每台机器将驱动程序放在同一位置.
您可以将驱动程序放置在路径中已列出的目录中,
也可以将其放置在目录中并将其添加到PATH
.
- 要查看
PATH
上已有哪些目录, 请打开命令提示符/终端并键入:
{{< tabpane code=false persistLang=false >}} {{% tab header="Bash" %}}
要查看PATH
上已经有哪些目录, 请打开Terminal并执行
echo $PATH
如果驱动程序的位置不在列出的目录中, 可以将新目录添加到PATH:
echo 'export PATH=$PATH:/path/to/driver' >> ~/.bash_profile
source ~/.bash_profile
您可以通过启动驱动程序来测试其是否被正确添加:
chromedriver
{{% /tab %}}
{{% tab header="Zsh" %}}
要查看PATH
上已经有哪些目录, 请打开Terminal并执行:
echo $PATH
如果驱动程序的位置不在列出的目录中, 可以将新目录添加到PATH:
echo 'export PATH=$PATH:/path/to/driver' >> ~/.zshenv
source ~/.zshenv
您可以通过启动驱动程序来测试其是否被正确添加:
chromedriver
{{% /tab %}} {{% tab header="Windows" %}}
要查看PATH
上已经有哪些目录, 请打开命令提示符并执行:
echo %PATH%
如果驱动程序的位置不在列出的目录中, 可以将新目录添加到PATH:
setx PATH "%PATH%;C:\WebDriver\bin"
您可以通过启动驱动程序来测试其是否被正确添加:
chromedriver.exe
{{% /tab %}} {{< /tabpane >}}
如果PATH
配置正确,
您将看到一些与驱动程序启动相关的输出:
Starting ChromeDriver 95.0.4638.54 (d31a821ec901f68d0d34ccdbaea45b4c86ce543e-refs/branch-heads/4638@{#871}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
想要重新控制命令提示符可以按下 Ctrl+C
与上面的选项3类似, 您需要手动下载驱动程序(有关链接, 请参阅快速参考 部分). 在代码中指定位置本身的优点是 不需要指出系统上的环境变量, 但缺点是使代码的灵活性大大降低.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
System.setProperty("webdriver.chrome.driver","/path/to/chromedriver"); ChromeDriver driver = new ChromeDriver();
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver from selenium.webdriver.chrome.service import Service
service = Service(executable_path="/path/to/chromedriver") driver = webdriver.Chrome(service=service)
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver(@"C:\WebDriver\bin");
{{< /tab >}}
{{< tab header="Ruby" >}}
service = Selenium::WebDriver::Service.chrome(path: '/path/to/chromedriver') driver = Selenium::WebDriver.for :chrome, service: service
{{< /tab >}}
{{< tab header="JavaScript" >}} const {Builder} = require('selenium-webdriver'); const chrome = require('selenium-webdriver/chrome');
const service = new chrome.ServiceBuilder('/path/to/chromedriver'); const driver = new Builder().forBrowser('chrome').setChromeService(service).build(); {{< /tab >}}
{{< tab header="Kotlin" >}}
import org.openqa.selenium.chrome.ChromeDriver
fun main(args: Array) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver") val driver = ChromeDriver() }
{{< /tab >}}
{{< /tabpane >}}
浏览器 | 支持的操作系统 | 维护者 | 下载 | 问题追溯 |
---|---|---|---|---|
Chromium/Chrome | Windows/macOS/Linux | 下载 | Issues | |
Firefox | Windows/macOS/Linux | Mozilla | 下载 | Issues |
Edge | Windows/macOS/Linux | Microsoft | 下载 | Issues |
Internet Explorer | Windows | Selenium Project | 下载 | Issues |
Safari | macOS High Sierra and newer | Apple | 内置 | Issues |
Note: The Opera driver no longer works with the latest functionality of Selenium and is currently officially unsupported.
[创建你的第一个Selenium脚本]({{< ref "first_script.md" >}})