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 小时前
UI自动化-POM封装
开发语言·python·selenium·自动化
林深现海9 小时前
Jetson Orin nano/nx刷机后无法打开chrome/firefox浏览器
前端·chrome·firefox
网络安全-杰克12 小时前
Jmeter压力测试工具安装与使用
自动化测试·软件测试·测试工具·jmeter·职场和发展
Wpa.wk13 小时前
接口自动化 - 接口鉴权处理常用方法
java·运维·测试工具·自动化·接口自动化
0思必得013 小时前
[Web自动化] Selenium获取元素的子元素
前端·爬虫·selenium·自动化·web自动化
Wpa.wk14 小时前
接口自动化 - 多环境统一文件配置 +多响应统一转换处理
运维·服务器·测试工具·自动化·接口自动化·统一配置
John_ToDebug14 小时前
引擎深处的漫游者:构建浏览器JavaScript引擎的哲学与技艺
javascript·chrome·js
@zulnger15 小时前
selenium 自动化测试工具实战项目(窗口切换)
selenium·测试工具·自动化
senijusene15 小时前
Linux软件编程: Linux 操作系统基础与shell脚本
linux·运维·chrome
0思必得01 天前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化