Unclickeable PDF download page Chrome Selenium - python

I wrote the following procedure, based on Selenium and Chrome, to download a PDF file to a defined folder, after performing some actions on a web app:
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : path_to_destination,
"plugins.always_open_pdf_externally": True
# Additional options I've tried but didn't work
#,"download.prompt_for_download": False,
# 'profile.default_content_setting_values.automatic_downloads': 1,
# "helperApps.neverAsk.saveToDisk": mime_types,
# "plugin.disable_full_page_plugin_for_types": mime_types
}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(
executable_path=executable_path,
chrome_options=chrome_options
)
However, as soon as I click on the link that, normally, allows to visualize the pdf, the following unclickable page is displayed:
As soon as I manually click on it, everything works fine and the file is correctly downloaded to the indicated folder ("path_to_destination");
I tried with:
driver.find_element_by_xpath("//*[contains(#id, 'open-button')]").click()
# Or
driver.find_element_by_xpath("//*[contains(#id, 'main-content')]").click()
Since the xpath is:
//*[#id="main-content"]/a
But it does not work.
How can I either avoid opening this second page or clicking on the "Apri" (= Open) button?
P.S. Using Firefox and the following options, everything works fine:
# Setup
profile = webdriver.FirefoxProfile()
mime_types = "application/pdf,application/vnd.adobe.xfdf,"\
"application/vnd.fdf,application/vnd.adobe.xdp+xml"
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", full_destination)
# For PDFs
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", mime_types)
profile.set_preference("plugin.disable_full_page_plugin_for_types", mime_types)
profile.set_preference("pdfjs.disabled", True)

Try adding the following to prefs:
"download.prompt_for_download": False

Related

Selenium Chromedriver not downloading to specified directory

I am trying to download some data off of the FanGraphs Leaderboards using selenium. I was using Firefox do to so, but Chrome is a bit faster, so I was trying to switch over to that. With Firefox, downloading the files worked find, but I have been having trouble switching over to Chrome.
Setting Up Chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.headless = False
os.makedirs("dist", exist_ok=True)
preferences = {
"profile.default_content_settings.popups": 0,
"download.default_directory": "dist/",
"directory_upgrade": True
}
chrome_options.add_experimental_option(
"prefs", preferences
)
self.browser = webdriver.Chrome(
chrome_options=chrome_options
)
Exporting Data
while True:
try:
WebDriverWait(self.browser, 20).until(
expected_conditions.element_to_be_clickable(
(By.ID, "LeaderBoard1_cmdCSV")
)
).click()
break
except exceptions.ElementClickInterceptedException:
self.__close_ad()
When ever I run the tests for my module, the CSV file ends up in C:/Users/UserDir/Downloads, rather than the dist/ folder in my current working directory. I double checked that the dist/ folder exists, and it does.
Specs
Python v3.9
selenium v3.141.0
Chromedriver v89.0.4389.23
Google Chrome v88.0.4324.190
I had this same problem and I fixed as
option.add_experimental_option("prefs", {'download.default_directory': f"{download_path}",
'download.prompt_for_download': False,
'download.directory_upgrade': True})
directory_upgrade may miss download. before.
Copy and paste this one
preferences = {
"profile.default_content_settings.popups": 0,
"download.default_directory": "dist/",
"download.directory_upgrade": True
}

how to switch download directory using selenium / firefox / python?

I know this question has been asked before...but I've tried multiple approaches and for some reason anything I download from the driver keeps going to my Downloads folder.
Basically I navigate to a website and download something by clicking a download link like so:
result.click()
This downloads the file fine. But I want to download it to a specific directory. I've tried doing these approaches to change the download directory:
driver = webdriver.Firefox()
profile = webdriver.FirefoxProfile()
driver.command_executor._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
Services.prefs.setBoolPref('browser.download.useDownloadDir', true);
Services.prefs.setStringPref('browser.download.dir', arguments[0]);
""", directory)
driver.execute("SET_CONTEXT", {"context": "content"})
and
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
Where directory is my desired location.
Neither of these worked...can anyone explain why or show me how to actually achieve this?
Thanks
As of 2021 the FirefoxProfile class used in earlier answers is deprecated, for the new selenium.webdriver.firefox.options.Options:
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", "./downloads")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(options=options)
You're initializing your browser with the default profile since you're not passing any profile argument to your webdriver.Firefox()
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(profile)
below options, it's working fine for me.
# set download options
download_path = DOWNLOADS_PATH
# 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
firefox_options.set_preference("browser.download.folderList", 2)
firefox_options.set_preference("browser.download.dir", download_path)

How to disable file download popup in firefox using selenium and python?

Im trying to download some reports from web page, using selenium and python
when i click on download link firefox shows save/open dialog
I have used firefox profile configuration as follows
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf,attachment/pdf")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,attachment/pdf")
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
self.browser = webdriver.Firefox(profile)
but still it shows message box,
i have changed
self.browser = webdriver.Firefox(firefox_profile=profile)
like above but no chance >
Can any one help me to resolve this?
I have a little helper function which works for me.
It uses chromedriver though.
def driver_download(location_for_download):
# options = Options()
# options.headless = True
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': location_for_download}
chrome_options.add_experimental_option('prefs', prefs)
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=chrome_options)
return driver
Note: For my particular application, I was unable to use the headless. But it should work if its a direct link to the file.

Headless Python Selenium MacOS to Click/Download Documents via Chromium

There's a lot on this topic. However, I have found nothing workable so far that involves using what is said in the title above and configurations listed below.
Here is what I am attempting to do: go to this webpage and click on the csv document icon for download (via xpath or css selectors). Either icon is fine - they download the same content.
The sourcecode below outlines what I have done so far. This script runs with no issues, but no document is downloaded - how do I possibly resolve this issue?
Note the following parameters for OS, Python, ChromeDriver, and Chrome configurations:
macOS Mojave v.10.14.6, Python v.3.7.3, ChromeDriver v.770386540, Chrome v.770386540
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": "SOME_PATH"}
options.add_experimental_option("prefs", prefs)
options.binary_location = 'PATH_TO_CHROME'
options.add_argument('headless')
# set the window size
options.add_argument('window-size=1200x600')
# initialize the driver
driver = webdriver.Chrome('PATH_TO_CHROME_DRIVER',
options=options)
page_url = 'http://webapps.rrc.texas.gov/eds/eds_searchUic.xhtml'
button = '//*[#id="SearchUicForm:searchTable_paginator_top"]/a[7]'
driver.get(page_url)
# wait up to 10 seconds for the elements to become available
driver.implicitly_wait(5)
driver.find_element_by_xpath(button).click()
You can comment this line of code options.add_argument('headless') and see what is happening in browser. It basically clicks the cvs icon and a download window pop up in browser so we need to handle this pop up window in order to download. We can add chrome options to prevent this.
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\xxx\downloads\Test",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome(chrome_options=options)

Python selenium chrome doesn't download with driver.get(url)

This code is supposed to download the sample pdf file but it only displays.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
"download.default_directory": r"/Users/ugur/Downloads/",
"download.prompt_for_download": True,
"download.directory_upgrade": False,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome(executable_path="/Users/ugur/Downloads/chromedriver",chrome_options=options)
driver.get('http://www.africau.edu/images/default/sample.pdf')
This is a demonstration, real website is different and it requires authentication so after running the initial part of the code I manually enter username and password and then run a for.
On your computer, open Chrome.
Navigate to chrome://settings
Go to advance settings.
Under “Privacy”, click Content settings.
Under “PDF Documents," check the box next to "Download PDF files instead of automatically opening.”

Categories