Downloading file to specified location with Selenium and python - python

Ok so far i have my programing going to the website i want to download link from and selecting it, then the firefox dialogue box shows up and i don't know what to do. i want to save this file to a folder on my desktop. I am using this for a nightly build so i need this to work. Please help.
Here is my code that grabs the download link from the website:
driver = web driver.Firefox()
driver.implicitly_wait(5)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]".click()

You need to make Firefox save this particular file type automatically.
This can be achieved by setting browser.helperApps.neverAsk.saveToDisk preference:
from selenium import webdriver
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 DESKTOP')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]").click()
More explanation:
browser.download.folderList tells it not to use default Downloads directory
browser.download.manager.showWhenStarting turns of showing download progress
browser.download.dir sets the directory for downloads
browser.helperApps.neverAsk.saveToDisk tells Firefox to automatically download the files of the selected mime-types
You can view all these preferences at about:config in the browser. There is also a very detailed documentation page available here: About:config entries.
Besides, instead of using xpath approach, I would use find_element_by_partial_link_text():
driver.find_element_by_partial_link_text("DEV.tgz").click()
Also see:
Access to file download dialog in Firefox
Firefox + Selenium WebDriver and download a csv file automatically

If the application is generated dynamically (mime-types) using Chrome browser will be a better approach since the Chrome will not open the file download pop-up.But multiple download option should be enabled if you need multiple downloads.

Related

How can I download dynamically the newest webrowser for firefox using selenium?

The main goal is to download web browser which is compatible with the current one.
This approach work so well for chromium and it's what I want to achive for gecko :
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
What to add to the below line to achieve the same results ? It doesn't work.
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

How to open browser with all credentials and settings already loaded

Hope everyone is doing okay 😊
Currently I’m trying to get info from a page where I have to sign in and then complete a Captcha (selecting different pictures) in order to proceed. The thing is that when I enter the page from my default browser (like manually clicking chrome) I don’t need to sign in and complete the captcha, because I am already signed in (I think because of the cookies?).
Every time I run selenium it opens a “blank” Chrome, is it possible to open a browser with all my data already loaded in order to skip the captcha?
I have tried different solutions explained here but with no success How to save and load cookies using Python + Selenium WebDriver
Thank you very much!
Yes there is a way: You can load your Chromebrowser with the chrome-profile saved on your computer. To do this you have to use webdriver.Chromeoptions().
options = webdriver.ChromeOptions()
options.add_argument(r'--user-data-dir=C:\Users\YourUser\AppData\Local\Google\Chrome\User Data\')
PATH = "/Users/YourUser/Desktop/chromedriver"
driver = webdriver.Chrome(PATH, options=options)
In your chrome-user, every cookie and stuff is saved and you no longer have a clean browser. Remember: There are still websites which can detect that your using selenium, but there are only a few...

How to access Google Chrome Extensions (Session Buddy) via Python?

I want to access Session Buddy using Python.
In my case "access" means to get all currently opened URLs from Chrome.
Session Buddy allows you to save all opened URLs into a .csv file.
To do so you need to "setup" a few things (simplified: press buttons) and then all URLs are downloaded to Chromes /Downloads directory.
I would like to fully automate this process though. This means python needs to access Session Buddy, initiate the download and then save the file to the directory you want it to.
I can't use requests or something though since an extension won't work using an URL. This is what the extension calls: chrome-extension://edacconmaakjimmfgnblocblbcdcpbko/main.html
In general, I don't necessarily want to use Session Buddy to get all the URLs, it just seems to be the easiest way..
So, in summary, I just want to ask: How can I automatically use Python to fetch all currently opened URLs in my Chrome Browser (using Session Buddy)?
I'm thankful for any kind of help.
You can use selenium webdriver and load .crx(extension) file to automate
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

how to get list of browser installed in a system using python?

I would like to write a script (in python) which scans the machine.(assumption is system has Linux) and retrieve the list of browser's installed. Looking for suggestions to implement it. I am using selenium to open links
browser = webdriver.Firefox()
Here we have to mention Firefox to open link in Firefox browser. What if user don't have Firefox in machine (chrome installed)?
I had already searched but haven't got any result.
P.S: If system has windows/Mac OS
A better approach: use try/except block
try:
browser = webdriver.Firefox()
browser.get('url')
except (IOException, Exception):
pass
It will help in cases where driver is not able to find the browser or there is some problem while launching.
You can scan all .desktop files in /usr/share/applications looking for WebBrowser in Categories field.

Unable to interact with dialog box using selenium in Python

I am using "Python-3.4.1 32 bit" on "Windows 7 64 bit" machine with "Selenium" using "Firefox 33". I am trying to automate downloading a "CSV" file from a secured website after furnishing the necessary credentials.
I have written the required code to navigate to the correct element to initiate the download of the file but I am stuck when Firefox gives the download box (Dialog box title: Opening export.csv, "Open with" and "Save File" options as radio buttons with "OK" and "Cancel" as buttons). I want the file to be downloaded automatically and for that I have tried the following codes without success:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir", "C:\\Users\\arun\\Downloads")
fp.set_preference("browser.download.downloadDir", "C:\\Users\\arun\\Downloads")
fp.set_preference("browser.download.defaultFolder", "C:\\Users\\arun\\Downloads")
fp.set_preference('browser.helperApps.neverAsk.saveToDisk',"text/csv")
fp.set_preference("browser.helperApps.neverAsk.openFile", "CONTENT-TYPE")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","CONTENT-TYPE")
driver = webdriver.Firefox(fp)
driver.get("http://website-url.com")
and then click on the button to start the download. But, it still shows the dialog box. I even tried handling it as an alert and tried:
alert = driver.switch_to_alert()
alert.accept()
but this always gave an exception of alert not found which led me to the conclusion that it might not be a JavaScript functionality after all but rather a web element.
What do I do to get this downloaded automatically without the dialog box interaction (the way it's handled in Google Chrome browsers)
Thanks for your help and time
Cheers!
You set browser.helperApps.neverAsk.saveToDisk to text/csv and then two lines later you set it to CONTENT-TYPE.
According to this page this preference is interpreted as a list of MIME types. CONTENT-TYPE is not a valid MIME type and it has no other special meaning. You want to remove the 2nd assignment to browser.helperApps.neverAsk.saveToDisk. You probably also don't want the assignment to browser.helperApps.neverAsk.openFile.
Once, your browser.helperApps.neverAsk.saveToDisk is set properly, you should not have to worry about accepting a dialog box.
Selenium doesn't allow interaction with dialog windows (Save As dialog windows, for example). selenium is a web driver, so it only interacts with the browser.
I recommend using pywinauto and pywin32. It is a form of windows automation.
The problem is that selenium will "pause" while the dialog window is up, so to achieve this, the only way I know would be to write two scripts. The first would be the selenium / web automation process, and the other one the windows automation using pywinauto and/or pywin32.
You can then call the windows automation script at the end of the selenium/web automation script by:
import subprocess
subprocess.call("python C:\Python27\Scripts\windowsAutomation.py", shell=True)
One more thing, I am using FireFox 31
You can try with Chrome chrome browser if all else fails. Chrome will not ask for download location but you have to enable multiple file download and set a save location

Categories

Resources