【Python自动化】Ubuntu24.04配置Selenium并测试

生信工作者学习Selenium 自动化也是十分必要的。今天跟AI聊了几个小时,终于给我写了个科研通的自动登录签到的Python脚本。中途AI提示我可以用requests 写,大家可以试下。

借助AI写自动化的一个小技巧,对于不熟悉或者我这种不懂网页源码的,可以把网页源码界面发给AI,让它给你识别其中的元素名称。

以下内容为我是我在个人阿里云上部署和测试Selenium 的步骤。

安装 Python 和 pip

Ubuntu 24.04 默认可能自带 Python 3,但最好确认:

复制代码
sudo apt update
sudo apt install python3 python3-pip -y
python3 --version
pip3 --version

依赖

sh 复制代码
sudo apt update
sudo apt install -y \
    fonts-liberation \
    libappindicator3-1 \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libcups2 \
    libdbus-1-3 \
    libnspr4 \
    libnss3 \
    libx11-6 \
    libx11-xcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxrandr2 \
    xdg-utils \
    libgbm1
  • linux上中文页面支持

用于中文支持,在签到成功界面截图。

sh 复制代码
sudo apt install -y fonts-noto-cjk fonts-noto-cjk-extra

安装 Selenium

  • 这里并没有使用虚拟环境进行安装。最好还是建立虚拟环境进行安装。
sh 复制代码
pip3 install --user selenium --upgrade --break-system-packages
pip3 show selenium
# Name: selenium
# Version: 4.35.0
# Summary: Official Python bindings for Selenium WebDriver
# Home-page: https://www.selenium.dev
# Author: 
# Author-email: 
# License: Apache-2.0
# Location: /root/.local/lib/python3.12/site-packages
# Requires: certifi, trio, trio-websocket, typing_extensions, urllib3, websocket-client
# Required-by:
  • Selenium 4 之后已经内置支持 Chrome DevTools Protocol(CDP),适合做全页截图。

安装 Google Chrome 或 Chromium

sh 复制代码
sudo apt install chromium-browser -y
chromium-browser --version

# Chromium 140.0.7339.127 snap

安装 ChromeDriver

方法 A:自动安装(推荐 Selenium 4.6+)

Selenium 4.6+ 可以直接通过 webdriver_manager 自动管理:

sh 复制代码
# pip3 install webdriver-manager
pip3 install --user webdriver-manager --upgrade --break-system-packages

方法 B:手动下载对应版本 ChromeDriver【实际使用】

https://developer.chrome.google.cn/docs/chromedriver/downloads/version-selection?hl=zh-cn

  1. 查看 Chrome/Chromium 版本:
shell 复制代码
google-chrome --version
# Google Chrome 140.0.7339.127
  1. 下载对应版本的 ChromeDriver:https://chromedriver.chromium.org/downloads
  2. 解压并移动到 /usr/local/bin/
shell 复制代码
wget https://storage.googleapis.com/chrome-for-testing-public/140.0.7339.82/linux64/chromedriver-linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver-linux64/chromedriver /usr/local/bin/
sudo chmod +x /usr/local/bin/chromedriver
chromedriver --version
# ChromeDriver 140.0.7339.82 (bc93617e21c39ed4afa6ce1c08554e5aa76d132d-refs/branch-heads/7339_41@{#5})

中文字体

  • 抓取签到网络截图
sh 复制代码
sudo apt install -y fonts-noto-cjk fonts-noto-cjk-extra

测试能否正常打开网页

  • 可能会有部分报错提示,属于正常
sh 复制代码
google-chrome --headless --no-sandbox --disable-gpu --enable-unsafe-swiftshader https://www.baidu.com

测试脚本

注意,linux与win10上的设置参数不一样,需要注意。

以下脚本为linux上的打开百度网页设置

sh 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

# linux测试
options.add_argument("--headless=new")          # 新版 Chrome 推荐写法
options.add_argument("--no-sandbox")            # 服务器必加
options.add_argument("--disable-dev-shm-usage") # 避免 /dev/shm 太小
options.add_argument("--disable-gpu")           # 禁用 GPU
options.add_argument("--disable-software-rasterizer") # 禁用软件渲染
options.add_argument("--disable-extensions")    # 禁用扩展
options.add_argument("--disable-features=VizDisplayCompositor")

driver = webdriver.Chrome(options=options)
driver.get("https://www.baidu.com")
print("网页标题:", driver.title)
driver.quit()
相关推荐
默_笙12 小时前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_4260039613 小时前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫13 小时前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技13 小时前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读13 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时14 小时前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊15 小时前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd12315 小时前
2026年第38周GitHub趋势周报
python·科技·github
IZero0715 小时前
Jev 与 Laya
python·语言模型