Skip to content

Instantly share code, notes, and snippets.

@aaronkirkman
Last active November 1, 2021 00:29
Show Gist options
  • Select an option

  • Save aaronkirkman/b61f4ce7c6e91bba718d0c7feaf042a5 to your computer and use it in GitHub Desktop.

Select an option

Save aaronkirkman/b61f4ce7c6e91bba718d0c7feaf042a5 to your computer and use it in GitHub Desktop.
Python code to download a file with Selenium
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
OUTPUT_DIR = "output"
URL = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
# trailing slash is necessary
prefs = {"download.default_directory": os.path.join(os.getcwd(), f"{OUTPUT_DIR}/"),
"directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False,
"plugins.always_open_pdf_externally": True}
options = Options()
options.add_experimental_option("prefs", prefs)
chromedriver_path = os.path.join(os.getcwd(), "chromedriver.exe")
driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
driver.get(URL)
driver.implicitly_wait(60)
time.sleep(30)
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment