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.
Related
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.
I have written a simple selenium code that downloads files into the custom directory. The code is
for firefox:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList",2)
# 0 for desktop
# 1 for default download folder
# 2 for specific folder
# You can specify directory by using profile.set_preference("browser.download.dir","<>")
profile.set_preference("browser.download.dir",dest_dir1)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
# If you don't have some download manager then you can remove these
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
driver=webdriver.Firefox(firefox_profile=profile,executable_path="geckodriver.exe")
Now, I want to create a similar type of script for chrome. I just want the chrome script to download into the dest_dir1. I don't see any options like webdriver.ChromeProfile similar to webdriver.FirefoxProfile().
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": "YOUR DOWNLOAD DIRECTORY",
"download.directory_upgrade": True,
"download.manager.showWhenStarting": False,
"download.manager.useWindow": False,
"helperApps.alwaysAsk.force":False,
"download.manager.showAlertOnComplete": False}
}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options, path="EXECUTABLE CHROME DRIVER PATH")
I have added few tags hope you understand how it works and build your own preference option.
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)
Using the following selenium/chrome preferences, I can download a file to the same folder that I run the original python file from, and it works properly (file saves as download.xls)
DOWNLOAD_DIR = r'/mnt/ssd/rl-scrape/files/dev/dl/'
options = Options()
options.headless = True
options.add_argument("--incognito")
options.add_argument("--window-size=1920,1200")
options.add_argument("--disable-extensions")
options.add_experimental_option("prefs", {
"download.default.directory": DOWNLOAD_DIR,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True})
I understand that I've typo'd download.default_directory above. If I correct it, the file gets downloaded to the desired directory, but as download.xls.crdownload and not download.xls
Chrome adds the .crdownload extension while the download is in progress. Once the file is complete, it will rename the file to its proper name. As long as you see .crdownload, the download is not complete.
Add a time delay for the download process to finish before it quits the window. Worked for me.
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!