selenium chrome centos7 设置download.default_directory不生效
时间: 2023-11-28 21:50:35 浏览: 265
如果你在 CentOS 7 上使用 Selenium 和 Chrome,且设置 `download.default_directory` 不生效,可能是因为 Chrome 在 headless 模式下没有权限访问文件系统。你可以尝试以下解决方案:
1. 给 Chrome 添加用户组权限
执行以下命令,将当前用户添加到 `chrome` 用户组中:
```
sudo usermod -a -G chrome $USER
```
然后重新启动 Chrome,试试能否下载文件。
2. 设置 Chrome 的命令行参数
在启动 Chrome 时,通过命令行参数来设置 `download.default_directory`。例如:
```python
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-infobars')
options.add_argument('--disable-popup-blocking')
options.add_argument('--start-maximized')
options.add_argument('--window-size=1920,1080')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-features=VizDisplayCompositor')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=TaskSchedulerFromIdle')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=AudioServiceOutOfProcess')
options.add_argument('--disable-features=VizDisplayCompositor')
options.add_argument('--disable-features=OutOfBlinkCors')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=AudioServiceOutOfProcess')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=TaskSchedulerFromIdle')
options.add_argument('--disable-features=OutOfBlinkCors')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=NetworkServiceInProcess')
options.add_argument('--disable-features=VizDisplayCompositor')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=TaskSchedulerFromIdle')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=OutOfBlinkCors')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=NetworkServiceInProcess')
options.add_argument('--disable-features=VizDisplayCompositor')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=TaskSchedulerFromIdle')
options.add_argument('--disable-features=OutOfBlinkCors')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=AudioServiceOutOfProcess')
options.add_argument('--disable-features=VizDisplayCompositor')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=NetworkServiceInProcess')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=TaskSchedulerFromIdle')
options.add_argument('--disable-features=OutOfBlinkCors')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=TaskSchedulerFromIdle')
options.add_argument('--disable-features=OutOfBlinkCors')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=RendererCodeIntegrityVerification')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=VizHitTestDrawQuad')
options.add_argument('download.default_directory=/path/to/download/directory')
driver = webdriver.Chrome(options=options)
```
其中,`/path/to/download/directory` 为你要下载文件的目录路径。
阅读全文