Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Last active August 17, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinThoma/dfc6e840059f3c03cbaf90d422310f1f to your computer and use it in GitHub Desktop.
Save MartinThoma/dfc6e840059f3c03cbaf90d422310f1f to your computer and use it in GitHub Desktop.
Show how disabling the PDF viewer does not work. Tested in Chromium Version 60.0.3112.78 (Developer Build) (64-bit)
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def get_browser():
"""
Get a chrome browser with disabled PDF viewer.
See https://stackoverflow.com/a/41891043/562769
"""
path_to_chromedriver = '/usr/lib/chromium-browser/chromedriver'
chrome_profile = webdriver.ChromeOptions()
profile = {"plugins.plugins_list": [{"enabled": False,
"name": "Chrome PDF Viewer"}]}
chrome_profile.add_experimental_option("prefs", profile)
browser = webdriver.Chrome(executable_path=path_to_chromedriver,
chrome_options=chrome_profile)
return browser
def download_file_example(browser):
browser.get("https://martin-thoma.com/author/martin-thoma/")
links = browser.find_elements_by_xpath('//a')
for link in links:
if link.get_attribute('href').endswith('.pdf'):
print(link.get_attribute("outerHTML"))
link.click()
break
browser = get_browser()
download_file_example(browser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment