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()
Related
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
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)
I want to change web driver preferences so I can bypass the save/open prompt that pops up when the Firefox webdriver clicks on the download button for a pdf I want.
I am setting the preferences for the Firefox web driver and passing it as a parameter "options" when I initialize the webdriver. It shows that the preferences I enter save into options.preferences but when I have selenium click on the download button on the website, the download prompt for the pdf still pops up.
def __init__(self):
options = webdriver.FirefoxOptions()
options.headless = False
print(options.preferences)
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", 'myDir')
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
print(options.preferences)
self.driver = webdriver.Firefox(options = options)
I plan to have this headless, but have it set to False for now. What would be causing the webdriver not to take in the preferences I passed into Options?
Fixed the issue. It seems that the firefox pdf previewer was bypassing the settings I was passing through. The line below blocked the preview of the pdf and allowed the automatic download to take effect.
options.set_preference("pdfjs.disabled", True)
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!
Trying to extract an image, successfully triggered "Save Image as..." dialog, but couldn't send any keys, is there a way to solve this problem?
driver = webdriver.Firefox()
actions = webdriver.ActionChains(driver)
actions.move_to_element(img).context_click(img).send_keys('v').perform()
time.sleep(2)
# and this line does not work
actions.send_keys('image.jpg').perform()
Only one step away from making everything works, what should I do?
This is a kind of popup you cannot control with selenium.
In this case you need to ask the browser to save the file automatically by tweaking it's preferences (aka desired capabilities):
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/path/to/file")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg")
driver = webdriver.Firefox(firefox_profile=profile)
where browser.helperApps.neverAsk.saveToDisk setting value should have a mime-type (or a comma-separated list of mime-types) of the files that should be downloaded automatically.
See also:
Access to file download dialog in Firefox