如何在Ubuntu上部署最新的Google Chrome和ChromeDriver

本章将帮助各位同学如何在Ubuntu及衍生版安装ChromeDriver和Selenium。

准备

执行以下命令以在系统上安装所需的软件包。这里 Xvfb(X 虚拟帧缓冲区)是用于类 UNIX 操作系统(例如 Linux)的内存显示服务器。它实现了没有任何显示的 X11 显示服务器协议。这对于 CI 服务等 CLI 应用程序很有帮助。

ruby 复制代码
$ sudo apt update 
$ sudo apt install -y unzip xvfb libxi6 libgconf-2-4 

安装 Chrome

ruby 复制代码
$ sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add 
$ sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list" 
$ sudo apt -y update 
$ sudo apt -y install google-chrome-stable 

安装 ChromeDriver

查看 Chrome版本

css 复制代码
$ google-chrome --version 
Google Chrome 122.0.6261.111

官网只有115及以下的支持版本 我找到一个文件可以找到最新版的 ChromeDriver。通过Wget、curl或aria2下载chromedriver_linux64.zip

解压

python 复制代码
$ unzip chromedriver_linux64.zip 

部署

shell 复制代码
$ sudo mv chromedriver /usr/bin/chromedriver 
$ sudo chown root:root /usr/bin/chromedriver 
$ sudo chmod +x /usr/bin/chromedriver 

Python selenium

代码

javascript 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

service = Service(executable_path='/usr/bin/chromedriver')
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(service=service, options=options)

driver.get('https://www.python.org/')
相关推荐
谢小飞20 小时前
如何让AI用一个下午开发上架Chrome插件助我摸鱼
前端·chrome
小溪彼岸1 天前
谷歌发布Gemini in Chrome,Chrome正式成为AI浏览器
chrome·google
小溪彼岸2 天前
谷歌上线Gemini in Chrome,想免费使用还需打怪升级
chrome·google
fthux3 天前
大道至简 - Juejin Notifier - 掘金消息通知小助手
chrome
元Y亨H4 天前
华硕主机重装 Ubuntu 系统完全指南
ubuntu
字节逆旅5 天前
ubuntu应用深度守护
ubuntu
简离11 天前
前端调试实战:基于 chrome://webrtc-internals/ 高效排查WebRTC问题
前端·chrome·webrtc
欧云服务器11 天前
怎么让脚本命令可以同时在centos、debian、ubuntu执行?
ubuntu·centos·debian
智渊AI11 天前
Ubuntu 20.04/22.04 下通过 NVM 安装 Node.js 22(LTS 稳定版)
ubuntu·node.js·vim
The️12 天前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互