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.
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.
But, the same code when executed in the VPS server doesn't download anything.
Things I've tried so far:
- locate any pdf downloaded by the script using
locate -i *.pdf
(It confirms no new pdfs were downloaded) - setting an environment variable by using:
export XDG_DOWNLOAD_DIR='path/to/dir'
- Running the command:
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)
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.