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()
相关推荐
惜.己4 小时前
selenium获取元素 出现的错误AttributeError: ‘TestPage‘ object has no attribute ‘driver‘
python·selenium·pycharm
四月友人A6 小时前
Chrome DevTools 深度挖掘:90%开发者都不知道的调试秘籍
前端·chrome
Ronin-Lotus7 小时前
上位机知识篇---脚本文件
linux·前端·chrome·终端命令·脚本文件
吃饱了想撑死8 小时前
Postman四种请求教程
测试工具·postman
东风西巷1 天前
猫眼浏览器:简约安全的 Chrome 内核增强版浏览器
前端·chrome·安全·电脑·软件需求
大白曾是少年1 天前
基于Postman进行http的请求和响应
测试工具·http·postman
成成成成成成果2 天前
揭秘动态测试:软件质量的实战防线
python·功能测试·测试工具·测试用例·可用性测试
卓码软件测评2 天前
软件测评中网站类测评测试使用的BurpSuite-Web安全测试流程
测试工具·安全·web安全
测试狗科研平台2 天前
TEM在量子点发光材料领域的应用-测试GO
测试工具·量子计算·材料工程