Automated Chromedriver PDF Download Brings Up Button? - python

I'm using Selenium to download an embedded pdf accessed through many complex layers of logins and other browser actions. I've set up my chromedriver with the following options per instruction from various other posts:
chromedriver = r'C:\Users\cj9250\AppData\Local\Continuum\anaconda3\chromedriver.exe'
download_dir = "C:\\Users\\CJ9250\\Downloads\\" # for linux/*nix, download_dir="/usr/Public"
options = webdriver.ChromeOptions()
profile = {
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
"download.default_directory": download_dir ,
"download.extensions_to_open": "applications/pdf",
"plugins.always_open_pdf_externally": True,
"download.prompt_for_download": False,
"safebrowsing.enabled": True
}
options.add_experimental_option("prefs", profile)
browser = webdriver.Chrome(chromedriver, chrome_options=options)
However, I get this box that I have to click before it downloads to my specified directory:
The 'Open' element doesn't have an xpath that I can find through the inspector. I'm guessing that this is some kind of internal security setting for the ChromeDriver but I can't find a way past it.
My end goal is just to download a embedded PDF in an open Selenium Test page, this seemed the only suggested course of action.
reportSho.do OPEN

I'm not sure why it worked but, I modified my profile variable to:
profile = {
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}], # Disable Chrome's PDF Viewer
"download.default_directory": download_dir ,
"download.extensions_to_open": "applications/pdf",
"safebrowsing.enabled": False
}
From there I was able to get a frame element on the page with the open button. One of the element attributes had a URL. When I instructed the browser to go to the URL, it downloaded the file to my specified directory!

Related

downloading pdf files with playwright

when I try to download a pdf using playwright, the programm open the pdf instead. In Selenium I used to change some settings in the browsers.
this is the script
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": r"C:\Users\ayoub\Desktop", #Change default directory for downloads
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})
I want to do the same in playwright or if there is a better solution I want to know it.
and thanks for your help.

Robocorp selenium fails to open pdf in chrome browser

I am trying to automate clicking through a website and currently have the following code
def open_site(self, url, headless=True, **kwargs):
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--disable-popup-blocking")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_argument('--disable-infobars')
options.add_argument("--disable-web-security")
options.add_argument("--allow-running-insecure-content")
options.add_experimental_option('prefs', {
"download.default_directory": str(Path(self.download_dir).resolve()), # Change default directory for downloads
"download.prompt_for_download": False, # To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True, # It will not show PDF directly in chrome
"download.extensions_to_open": "applications/pdf",
"profile.default_content_settings.popups": 1
})
self.create_webdriver('Chrome', desired_capabilities=options.to_capabilities(), executable_path="/path/to/chrome/driver")
self.go_to(url=url)
However, when the website opens I get stuck at the point of loading the pdf. It seems as though chrome fails on the aspx part. The website does load the pdf in Safari but this can only be used locally. Firefox isint supported at all by the website. Chrome is the only option. pdf image and click through image. Any assistance would be appreciated.

Selenium webdriver - prefs ignored when using specific Chrome profile

I read a lot of answers about setting a download directory for Chrome, but couldn't find anything when you want to save a file to a specific directory, different from the default one, and you are using a specific profile.
The below code works fine, and when I download a file, it gets saved to the intended downloadFolderPath:
downloadFolderPath = 'P:\\reports\\attachments'
options = webdriver.ChromeOptions()
preferences = {"download.default_directory": downloadFolderPath,
"directory_upgrade": True,
"safebrowsing.enabled": True }
options.add_experimental_option("prefs", preferences)
options.headless = False
browser = webdriver.Chrome(executable_path='chromedriver.exe', options=options)
# browse to the page, click download, etc.
But when I try to do the same, this time using a specific Chrome profile, by adding user-data-dir and profile-directory arguments:
downloadFolderPath = 'P:\\reports\\attachments'
options = webdriver.ChromeOptions()
options.add_argument(r'--user-data-dir=C:\\Users\\my.name\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument(r'--profile-directory=Profile 5')
preferences = {
"download.default_directory": downloadFolderPath,
"directory_upgrade": True,
"safebrowsing.enabled": True
}
options.add_experimental_option("prefs", preferences)
options.headless = False
browser = webdriver.Chrome(executable_path='chromedriver.exe', options=options)
# browse the page and download the file
Chrome indeed gets opened with the chosen profile, but the file that I download gets saved under the user profile default directory, C:\Users\my.name\Downloads instead of the one that I defined (P:\reports\attachments). It seems therefore that the preferences passed with options.add_experimental_option("prefs", preferences) are ignored when a profile different from the default one is used.
Or is there any different argument that needs to be used when working with specific profiles?
For info, I am on Windows 10, Python 3.9, using Chrome 95.0.4638.54 and chromedriver.exe is 95.0.4638.17
Thanks

File dialogue not getting close after file is downloaded

I have written a code in python selenium that will login to Jira and then open another URL that is to download a report file.
Step 1- driver.get(jira.com) # I will login on this page
Step 2- driver.get('Another Jira URL.com\file.csv?somthing something....')# This URL will give me a csv file to download if I will put this in browser directly.
after step 2 It will open file dialogue it's downloading file in .temp format in download folder and after driver.quit() not closing file dialogue.
You need to set prompt_for_download is set to false, so that you would not see that download prompt at all. Create an options and pass them like below and then create object of chromedriver
Sample code :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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(executable_path = driver_path, options = options)

How to keep Selenium webdriver minimized throughout the process?

I developed a bot to download some files in the background, while working. But every time a download is made, the webdriver screen appears on the screen and I have to manually minimize it. Any suggestion?
options = Options()
options.add_experimental_option("prefs", {
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
"download.default_directory":"D:\Download",
"download.extensions_to_open": "applications/pdf",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False,
"profile.default_content_setting_values.notifications" : 2
})
driver = webdriver.Chrome('chromedriver',chrome_options=options)
driver.get("https://www.copel.com/AgenciaWeb/autenticar/loginCliente.do")
headless is not a option in my case
As mentioned here: Selenium Web-driver Documentation
You can minimize or maximize your browser window using minimize_window() and maximize_window() functions.
Adding this line solves your problem:
driver.minimize_window()
But only adding this may not help if your website opens a new tab after clicking on some link. So, adding these lines will ensure that your script is working with last opened tab and not the first tab. Add these lines in your script after every click event which results in opening a new tab.
win_list = driver.window_handles
driver.switch_to.window(win_list[-1])
For example, Here, l1 will throw Error if we don't switch to last opened tab.
driver.get("https://sites.google.com/a/chromium.org/chromedriver/home")
l0 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div[2]/div/table/tbody/tr/td[2]/div/div[3]/div/table/tbody/tr/td/div/h2/font/a')
l0.click() # This click event results in opening a new tab
win_list = driver.window_handles # This gives the list of all tabs
driver.switch_to.window(win_list[-1]) # To switch to the last tab opened
l1 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div[2]/div/table/tbody/tr/td[1]/div/div/ul/li[2]/div/a')
l1.click()

Categories

Resources