linux安装selenium步骤

1,安装selenium模块

1
pip3 install selenium

2,安装谷歌浏览器

1
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -y

3
安装chromedriver
1)运行下面命令查看浏览器版本
google-chrome --version

 出现这个代表谷歌浏览器安装成功

2)谷歌浏览器版本最新版124及以后得版本在这里下载

1
https://googlechromelabs.github.io/chrome-for-testing/#canary

  

 本地下载好解压之后放到服务器任意位置即可,记得加上运行权限

1
chmod +x chromedriver

最后一步上代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
 
# 指定驱动路径
driver_path = '/mnt/chrome/chromedriver'
 
# 创建ChromeOptions对象
chrome_options = Options()
 
# 添加Chrome启动参数
chrome_options.add_argument("--headless")   #这两个选项务必加上
chrome_options.add_argument('--no-sandbox')
 
# 创建Service对象并传入ChromeOptions
service = Service(driver_path)
 
# 创建WebDriver对象
driver = webdriver.Chrome(service=service, options=chrome_options)
 
# 打开网页
driver.get('https://www.baidu.com/')
 
# 输出网页源代码
print(driver.page_source)
 
# 关闭浏览器
driver.quit()

 

posted @   lvye001  阅读(793)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示