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 小时前
JADX与AI结合的实操指南:从工具配置到APK分析
人工智能·测试工具·安全性测试·jadx
AALoveTouch1 天前
某麦网抢票:基于Wireshark协议分析
网络·测试工具·wireshark
x***r1511 天前
Wireshark-4.4.2-x64安装步骤详解(附网络抓包与分析入门教程)
网络·测试工具·wireshark
NEKGod1 天前
Linux 文件篡改审计(auditctl 实战指南)
linux·运维·chrome
Waoooo19991 天前
谷歌云配置嵌套虚拟化
前端·chrome
Cd ...1 天前
RobotFramework Selenium与Browser常用关键字对比
自动化测试·selenium·robotframework·playwright·browser
hresh1 天前
两个 Chrome 窗口各 20 多个 tab 后,我把 tab-out 改成了更顺手的 TabNest
前端·chrome·后端
LT10157974441 天前
2026年性能测试平台信创适配评测:国产化场景落地能力解析
测试工具·性能优化
爆更小哇1 天前
pytest集成Allure报告教程
python·测试工具·pytest·接口测试·allure