「Selenium」- 搭建本地环境(Linux)

  CREATED BY JENKINSBOT

安装 Selenium 工具(快速开始)

Selenium 的工作流程:Python Lib => WebDriver => Web Browser

因此我们需要进行以下设置:
1)安装 Python Selenium 库,用于操作 WebDriver 程序的工具类库;
2)我们需要 WebDriver 程序,该工具用于操作浏览器;

注意事项:
1)除了 Python Selenum 库,其实还有 Java C# Ruby 等等库,都可以操作 WebDriver 程序。
2)由于我们使用 Python 语言,所以这里只介绍 Python 类库;

第一步、安装 Python Selenum 类库

// 留意自己使用的 Python 版本

# pip install selenium
# pip3.7 install selenium

// 我们使用 Debian 10 发行版,推荐 APT 安装

apt-get install python3-selenium

第二步、安装 WebDriver 程序

需要做两件事情:
1)下载与浏览器对应的 WebDriver 程序,比如:Chrome 需要下载 chromedriver 二进制程序;Firefox 需要下载 geckodriver 二进制程序;
2)将 WebDriver 程序放入系统路径,以便被搜索到。因为 Python Selenum 库需要执行该二进制程序,以便操作浏览器;

Chromium

我们这里以 Debian 10 的 Chromium 为例:

# chromium --version
Chromium 83.0.4103.116 built on Debian 10.4, running on Debian 10.5

// 方法一、下载安装

# wget -O /tmp/chromedriver_linux64.zip \
    https://chromedriver.storage.googleapis.com/83.0.4103.39/chromedriver_linux64.zip

# unzip -x /tmp/chromedriver_linux64.zip -d /usr/local/bin

# chromedriver
Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416}) 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

// 方法二、我们使用 Debian 10 发行版,推荐 APT 安装

# apt-get install chromium-driver

# chromedriver
Starting ChromeDriver 83.0.4103.116 (8f0c18b4dca9b6699eb629be0f51810c24fb6428-refs/branch-heads/4103@{#716}) 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.

第三步、编写自动化测试代码,以打开网页

使用 Python 打开页面:

from selenium.webdriver import Chrome

driver = Chrome()
driver.get("https://example.com")

使用 Java 打开页面:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

WebDriver driver = new ChromeDriver();
driver.get("https://example.com")

参考文献

Selenium installation :: Documentation for Selenium
Driver requirements :: Documentation for Selenium
Downloads – ChromeDriver – WebDriver for Chrome