简体   繁体   English

Selenium Python 在VPS中设置Chrome默认下载目录

[英]Selenium Python Set Chrome Default Download directory in VPS

I'm trying to download a pdf using the latest google-chrome & chromedriver on a Ubuntu 16.04 LTS VPS server with the following code.我正在尝试使用以下代码在 Ubuntu 16.04 LTS VPS 服务器上使用最新的 google-chrome & chromedriver 下载 pdf。

import json
import time
from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1768, 1368))
display.start()
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--disable-logging")
chrome_options.add_argument("--log-level=3")
chrome_options.add_argument("--kiosk-printing")
appState = {
    "recentDestinations": [{"id": "Save as PDF", "origin": "local"}],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
}

prefs = {
    "printing.print_preview_sticky_settings.appState": json.dumps(appState),
    "download": {
        "default_directory": "/path/to/dir/",
        "prompt_for_download": False,
        "directory_upgrade": True,
    },
}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(
    "https://www.adobe.com/content/dam/acom/en/accessibility/products/acrobat/pdfs/acrobat-x-accessibility-checker.pdf"
)
time.sleep(10)
driver.execute_script("window.print();")
time.sleep(30)
driver.quit()
display.stop()

When I test the above code locally, it downloads the file in the system's default download directory instead of path/to/dir but downloads the file anyway.当我在本地测试上面的代码时,它会在系统的默认下载目录而不是path/to/dir下载文件,但无论如何都会下载文件。

But, the same code when executed in the VPS server doesn't download anything.但是,同样的代码在 VPS 服务器上执行时不会下载任何东西。

Things I've tried so far:到目前为止我尝试过的事情:

  • locate any pdf downloaded by the script using locate -i *.pdf (It confirms no new pdfs were downloaded)使用locate -i *.pdf脚本下载的任何 pdf(确认没有下载新的 pdf)
  • setting an environment variable by using: export XDG_DOWNLOAD_DIR='path/to/dir'使用以下命令设置环境变量: export XDG_DOWNLOAD_DIR='path/to/dir'
  • Running the command: xdg-user-dirs-update --set DOWNLOAD path/to/dir运行命令: xdg-user-dirs-update --set DOWNLOAD path/to/dir
  • verified default download dir is set using the command: xdg-user-dir DOWNLOAD (It shows system's default download folder)使用以下命令设置已验证的默认下载目录: xdg-user-dir DOWNLOAD (它显示系统的默认下载文件夹)

But nothing worked so far, any help will be appreciated!但到目前为止没有任何效果,任何帮助将不胜感激!

NOTE: I know it is possible to download the file by making a GET request using modules like requests , urllib3 , etc. I'm just looking for a selenium based solution.注意:我知道可以通过使用requestsurllib3等模块发出 GET 请求来下载文件。我只是在寻找基于 selenium 的解决方案。

I ran into a similar problem and was able to find the following solution:我遇到了类似的问题,并找到了以下解决方案:

The download.default_directory setting is only for downloaded content. download.default_directory 设置仅适用于下载的内容。 Chrome treats files saved on the page differently. Chrome 以不同方式处理保存在页面上的文件。 To change the default folder for a printout of the page, simply set the savefile.default_directory value instead.要更改页面打印输出的默认文件夹,只需设置 savefile.default_directory 值即可。

In order to update the download directory for window printing, update your prefs with this:为了更新 window 打印的下载目录,请使用以下内容更新您的首选项:

prefs = {
    "printing.print_preview_sticky_settings.appState": json.dumps(appState),
    "download": {
        "default_directory": "/path/to/dir/",
        "prompt_for_download": False,
        "directory_upgrade": True,
    },
    "savefile.default_directory": "path/to/dir/"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM