在 Linux 上执行 Selenium,首先需要安装 Python 和相应的库。以下是具体步骤:
1. 安装 Python:
```bash
sudo apt-get update
sudo apt-get install python3
```
2. 安装 Selenium 库:
```bash
pip3 install selenium
```
3. 下载浏览器驱动程序。以 Chrome 浏览器为例,下载对应版本的 ChromeDriver(https://sites.google.com/a/chromium.org/chromedriver/downloads):
```bash
wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
```
4. 编写一个简单的 Python 脚本来使用 Selenium:
```python
from selenium import webdriver
# 创建一个 Chrome WebDriver 实例
driver = webdriver.Chrome()
# 访问指定的 URL
driver.get("https://www.example.com")
# 关闭浏览器窗口
driver.quit()
```
5. 保存上述脚本为 `test_selenium.py`,然后在终端中运行:
```bash
python3 test_selenium.py
```
这样就可以在 Linux 上执行 Selenium 了。