selenium chrome centos7 设置download.default_directory 目录路径不生效
时间: 2023-11-28 14:50:41 浏览: 37
如果 `download.default_directory` 在 Chrome 中未设置为预期目录,则可能是由于 SELinux 导致的问题。SELinux 可能会阻止 Chrome 访问该目录。要解决此问题,请尝试以下步骤:
1. 禁用 SELinux:
```
setenforce 0
```
2. 重新启动 Chrome 并检查是否已设置默认下载目录。
如果您不想完全禁用 SELinux,您可以使用以下步骤调整 SELinux 策略以允许 Chrome 访问特定目录:
1. 查看 SELinux 上下文:
```
ls -Z /path/to/download/directory
```
2. 如果上下文不是 `system_u:object_r:user_home_t:s0`,请将其更改为该上下文:
```
chcon -t user_home_t /path/to/download/directory
```
3. 然后,使用以下命令将 SELinux 策略设置为允许 Chrome 访问该目录:
```
setsebool -P unconfined_exec_content_tty true
```
4. 最后,重新启动 Chrome 并检查是否已设置默认下载目录。
希望这些步骤能够帮助您解决问题。
相关问题
selenium chrome centos7 设置download.default_directory不生效
如果你在 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` 为你要下载文件的目录路径。
selenium savefile.default_directory和dowload.default_directory
`selenium`中`savefile.default_directory`和`download.default_directory`是两个不同的属性。
`savefile.default_directory`属性用于设置文件保存的默认目录,而`download.default_directory`属性用于设置文件下载的默认目录。
具体来说,`savefile.default_directory`属性是在使用`selenium`的`send_keys`方法上传文件时使用的,默认情况下,该属性的值为空字符串,即文件将保存在浏览器默认的下载目录中。如果要设置文件保存的目录,可以使用如下代码:
```python
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {'savefile.default_directory': '/path/to/directory'}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
```
而`download.default_directory`属性用于设置文件下载的默认目录,可以使用如下代码:
```python
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {'download.default_directory': '/path/to/directory'}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
```
注意,在设置以上属性时,需要将`options`添加到`webdriver.Chrome()`方法中创建`driver`对象。