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()
相关推荐
0思必得03 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
llilian_1613 小时前
信号发生器 多通道多功能脉冲信号发生器应用解决方案 多功能脉冲发生器
功能测试·单片机·嵌入式硬件·测试工具
2401_8590490814 小时前
git submodule update --init --recursive无法拉取解决
前端·chrome·git
程序员小远14 小时前
使用Postman进行一次完整的接口测试
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
我的xiaodoujiao15 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
生活很暖很治愈15 小时前
GUI自动化测试[3]——控件&数鼠标操作
windows·python·功能测试·测试工具
我的xiaodoujiao17 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
Tom·Ge18 小时前
在macOS上安装OpenClaw并实现Chrome网站自动化测试
chrome·macos·策略模式
uyeonashi19 小时前
Boost搜索引擎测试报告
功能测试·selenium·搜索引擎
@zulnger21 小时前
selenium 自动化测试工具实战项目(订单)
selenium·测试工具·自动化