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()
相关推荐
测试老哥8 小时前
软件测试:测试用例的设计
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
程序员杰哥12 小时前
Python自动化测试之线上流量回放:录制、打标、压测与平台选择
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
2501_9240641113 小时前
2025数据库性能测试工具:Utest、JMeter、HammerDB 等主流方案推荐
数据库·测试工具·jmeter·数据库性能测试·数据库负载测试·数据库压测工具·jmeter 压力测试
卖个几把萌13 小时前
【08】JMeter从文本中读取多个参数
测试工具·jmeter
神秘剑客_CN15 小时前
deepin安装chrome
chrome·deepin
Sunhen_Qiletian20 小时前
python语言应用实战--------网络爬虫篇 第二篇(selenium库)
爬虫·python·selenium
inter_peng1 天前
【DICOM网络问题】基于tcpdump和wireshark解决服务器无法连接打印机打印胶片的问题
测试工具·wireshark·tcpdump
BINGCHN1 天前
流量分析入门(二):wireshark的使用
网络·测试工具·wireshark
卓码软件测评1 天前
自动化软件API测试部署Postman和Jenkins集成
测试工具·自动化·jenkins·测试用例·postman·软件需求
陈辛chenxin1 天前
【接口测试】Postman教程
python·selenium·测试工具·postman·可用性测试