Python Selenium, Firefox profiles - python

I am new to python selenium and I need more explanation on the effect of the below code on selenium Firefox profile.
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))

Firefox browser is by default asking if to save or to open the file when user tries to download a file.
To prevent Firefox opening that dialog we can predefine not to ask this for predefined types of files.
Each file type should be mentioned explicitly with such setting in order to not ask when downloading of such a kind of file initiated.

Basically when setting up your Firefox profile you add a call to set the property browser.helperApps.neverAsk.saveToDisk like this.
You won't face download pop up.
so basically it means that download any file and save it to the desired location using Selenium Webdriver.

Related

What is the correct directory for user-data-dir in Selenium chrome.options

I'm trying to do some chrome automation using selenium on python, and I want to have my login info saved for youtube, so that I don't have to automate the login process. I read that the way to have chrome read my saved info is by using the user-data-dir option, but I can't get it to work.
I have looked at some questions here, such as this, and they seem to indicate that the correct usage of the argument user-data-dir in selenium for chrome is the profile path inside: C:\Users\adassa\AppData\Local\Google\Chrome\User Data. Such as Default or Profile 1. I can't seem to get the intended result using this kind of path. The only path that works for me is the User Data folder itself.
So when I do:
chrome_options.add_argument("--user-data-dir=" + r'C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data')
The chrome instance is opened with the preferences set for the default profile. But that only works correctly when I close all other instances of chrome, otherwise it raises the error: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Which I believe is caused by my manual chrome instance blocking access to the info on the folder because I'm using it. I read that I can create a new chrome profile, so that it doesn't conflict with the one I normally use, but if I create a new profile manually on chrome, and use the profile path, as in:
chrome_options.add_argument("--user-data-dir=" + r'C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1')
the new instance doesn't have the info I saved to the profile. That happens even if I use as path the User Data\Default folder. I have also tried different combinations using the option profile-dir, with no success.
After reading the docs, I tried using a folder that does not exist. In that case selenium does create the new folder, but when I try to login to youtube trough that selenium created instance, in order to save the info for the browser to use on the next time, I get this message (translation mine):
It was not possible to login. This browser or app may not be safe.
I would love to find a way to have my data saved so that I don't have to login everytime, and also not have to close all other chrome instances when running my script. Any help is appreciated.
chrome_options.add_argument("--user-data-dir=C:\\Users\\adassa\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument("--profile-directory=Profile 1")
as the name says usr-dir is for user-data folder and profile-directory is to specify the profile.
if profile folder is default , you don't have to specify that chrome takes it by default else specify it "--profile-directory=Profile 1"

How do I get python to use a firefox addon to download images from a local file

I am trying to get python to open a local html file in firefox and then use a addon called "download all" to download the images to a specific folder. I am not able to scrape them for some weird reason. If I can't do it this way I would want to use xpath to do it since the image links are laid out in tables. Is this possible?
You can't click on the element since it's not a web page element. However you can create a profile for firefox and include addons in that profile that is launched by the webdriver applications. This will allow you to have access to Firebug or other addons. you can set a profile and extend the profile with the addon api is like so:
File file = new File("addonName.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.addOn.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);
Thanks

Is is possible to load profile extension in chromedriver session?

I develop a program, which checks browser plugin behaviour [ in python & selenium ]. The plugin is a black box to me I just have it installed in browsers.
For example, in Firefox, i've installed it to my profile and force webdriver to open firefox with desired profile. I've made the same witch chrome, and it obviously loads the profile, but not the extension.
opt = webdriver.ChromeOptions()
opt.add_argument("--user-data-dir=C:\\Users\\..\\Google\\Chrome\\User Data\\Profile 2\\")
driver = webdriver.Chrome("C:\\...\\chromedriver.exe", chrome_options=opt)
Is there any way how to make this functionallity work? Or is the problem, that the extension are bound to the logged user, not profile of browser?
What I need to do is to execute chrome with specified url, let the plugin to analyze the site and then check plugin's output.
Another problem is that I am not the one who will use the program, so I wouldn't like to start force user to pack crx extenstion (or is there a simple way to this?). My requirments is that thare is one or two items in configuration file, like
chrome-profile: path/to/profile
Thank you for any advice.

selenium popup save file issue using python

I am working on a html with selenium. After clicking the last link, pop up comes which says as save a file.
using selenium I am recording all the events and then generating the selenium RC script.
I want to know that how to get the pop up file from code using python?
In the case of saving a file, you can get around the popup box by configuring the options of your browser profile. See this answer for an explanation using Firefox. General idea is that you need to tell Firefox itself to not prompt when saving files of certain types. Note that this will result in the file being saved somewhere, but you can also control where it goes in case you want to delete the file (or handle it separately in Python).
Webdriver cannot communicate with the browser modal popup.
But this can be done, check out the below link for your answer
http://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-impossible/

Handling "Download" window by Firefox WebDriver

I'm experimenting with Firefox's WebDriver and I'd like to ask if it is possible to handle "Download" window (to accept or decline incoming download request)?
For example, simple piece of code:
import selenium.firefox.webdriver
dr = selenium.firefox.webdriver.WebDriver()
# Firefox is showed up.
# Let's say I'd want to download python.
dr.get('http://python.org/ftp/python/3.1.3/python-3.1.3.msi')
# Download window is showed up.
# How could I accept the download request?
# As I understand, the method below should return
# two handles but I get only main window's handle.
handles = dr.get_window_handles()
# Seems like WebDriver cannot "see" this popup.
I've experimented with this a little bit but haven't found the solution yet. I'd really appreciate any hint.
Many thanks,
- V
One solution to this is changing WebDriver's Firefox profile to automatically download some MIME types to a given directory.
I'm not sure how (or if) this is exposed in Python, but it's mentioned on the Ruby bindings page on the Selenium wiki (under "Tweaking Firefox preferences").
I don't think that this is the sort of thing that WebDriver was built for, but I'll take a crack at it. There is nothing built into the Firefox WebDriver to handle this specific case, but there are a few approaches you may take.
You can open FF with the profile that your WebDriver script uses and edit the preferences to always save the file instead of asking (Options > Applications > Windows Installer Package - set to "Save File"). Now, however, there's no way to tell that the file is downloading from the browser unless you get redirected to a 404 page. If not, you can check if the file exists in the Downloads directory for the same profile (Options > Main > Donwloads). If it's still in the process of downloading, the filename will be WhateverFileName.ext.part
Your other option is to use the non-visual HTMLUnit driver, navigate to the download link, click it, and the get the page source (will be the contents of the file). This works with textual files, I can't guarantee that it will work similarly for binaries, nor do I know how it will be encoded in such a case.
Best of luck
i came across this when i was trying to download a file using capybara
and got halted by the download prompt
SeleniumHQ : Selenium WebDriver
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = "/Downloads"
profile['browser.download.folderList'] = 2
profile['browser.helperApps.neverAsk.saveToDisk'] = "audio/wav"
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.navigate.to('http://www.address.com/file.wav')
this just downloads the file into the directory specified, no prompt :)
the other option that i came across was
Determining file MIME types to autosave using Firefox & Watir-WebDriver
i have tried watir before and it proved very useful

Categories