ubuntu24.04安装selenium、chrome、chromedriver

实验环境:kaggle notebook、colab notebook

1、安装chrome

复制代码
!wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

!sudo dpkg -i google-chrome-stable_current_amd64.deb

!sudo apt-get install -f

!export QT_QPA_PLATFORM=offscreen

!sudo apt-get install -f

!sudo dpkg -i google-chrome-stable_current_amd64.deb

!google-chrome --version

安装字体

复制代码
!sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

2、安装chromedriver

复制代码
!wget https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.183/linux64/chromedriver-linux64.zip

!unzip chromedriver-linux64.zip

!sudo mv chromedriver-linux64 /usr/local/bin

!sudo chmod +x /usr/local/bin/chromedriver-linux64/chromedriver

3、安装selenium

复制代码
!pip install selenium

4、测试脚本(jupyter notebook版,无头浏览器模式)

复制代码
import tempfile
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
#from webdrivermanager_cn import ChromeDriverManager
import time

# 自动下载驱动
#driver_path = ChromeDriverManager().install()

# 创建临时用户数据目录
user_data_dir = tempfile.mkdtemp()

# 设置 Chrome 启动参数
options = Options()
options.add_argument(f"--user-data-dir={user_data_dir}")
options.add_argument("--no-sandbox")          # Linux 通常需要
options.add_argument("--disable-dev-shm-usage")  # Linux 通常需要
options.add_argument("--headless")            # 如不需要图形界面,可添加此项

# 启动浏览器
service = Service(executable_path='/usr/local/bin/chromedriver-linux64/chromedriver')
driver = webdriver.Chrome(service=service, options=options)

# 使用浏览器
driver.get("https://www.baidu.com")
print(driver.title)
time.sleep(3)
#截屏
driver.save_screenshot(filename="baidu.png")
time.sleep(3)

driver.quit()
相关推荐
辣香牛肉面2 小时前
Wireshark v4.6.2 开源免费网络嗅探抓包工具中文便携版
网络·测试工具·wireshark
龙飞053 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
zhengfei61119 小时前
面向攻击性安全专业人员的一体化浏览器扩展程序[特殊字符]
前端·chrome·safari
测试工程师成长之路19 小时前
Serenity BDD 框架:Java + Selenium 全面指南(2026 最新)
java·开发语言·selenium
may_一一19 小时前
xpath定位:selenium和playwrightAnt Design / 表单类页面)
selenium·测试工具
daopuyun20 小时前
CNAS/CMA软件检测实验室源代码漏洞测试工具选型要求与比对
软件测试·测试工具·软件检测·cnas认可·cma认定
老师用之于民20 小时前
【DAY21】Linux软件编程基础&Shell 命令、脚本及系统管理实操
linux·运维·chrome·经验分享·笔记·ubuntu
扶苏100221 小时前
vue使用event.dataTransfer实现A容器数据拖拽复制到到B容器
前端·vue.js·chrome
Wpa.wk21 小时前
接口自动化测试 - 请求构造和响应断言 -Rest-assure
开发语言·python·测试工具·接口自动化
AI_56781 天前
Postman接口测试提速技巧:批量请求+智能断言实践
测试工具·lua·postman