Setting selenium to use custom profile, but it keeps opening with default

Question:

I am trying to use python and selenium to automate some tasks in firefox. When I download a file, that pop up comes up asking if you want to open or save, and a check box for do this every time with this kind of file. I have found that check box does not work unless you install the add on Web Page Fixer. I have that installed normally, but when I use python + selenium it uses a profile with no add ons.

The internet has instructed me to create another profile by closing Firefox, opening /Applications/Utilities, then typing the command:

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p

I then create a new profile that I will use with selenium. I set the name and change the folder name. The profile name is “PTI_Auto_Profile”. The folder path displays as follows:

/users/User/Library/Application Support/Firefox/Profiles/Selenium/

When I am done. I click ‘Start Firefox’, and the following error appears on my terminal screen.

2013-04-11 11:57:30.422 firefox-bin[2248:707] invalid drawable
conf-room:~ User$ 2013-04-11 11:58:00.350 firefox-bin[2251:303] invalid drawable

I’ve tried the following to no success.

profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(firefox_profile=profile) 

No error, default user.

profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(profile) 

No error, default user.

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv/xls")

driver = webdriver.Firefox(firefox_profile=fp)

Error:
fp.set_preference(“browser.download.dir”,getcwd())
NameError: name ‘getcwd’ is not defined

Any ideas on what I am doing wrong? Thank you!

p.s. I am using mac os x 10.8.2, python 2.7, firefox 20

SOLUTION PROVIDED BY Corey Goldberg. This should work for all excel versions.

import os
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', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
driver = webdriver.Firefox(profile)

Answers:

Error: fp.set_preference(“browser.download.dir”,getcwd()) NameError:
name ‘getcwd’ is not defined

getcwd() is not defined. So I assume you want the getcwd from the os module:

add: import os , and then invoke with os.getcwd().

or you could just add the import for this function:
from os import getcwd

your example with the proper imports included:

import os
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', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls')
driver = webdriver.Firefox(profile)
Answered By: Corey Goldberg

I did the following:

Open Profile Directory

Or:

Linux: ls -d /home/$USER/.mozilla/firefox/*.default/ to see user profile directories

Mac: ls -d ~/Library/Application Support/Firefox/Profiles/*

Output:

/home/jmunsch/.mozilla/firefox/xfoyzfsb.default/
/home/jmunsch/.mozilla/firefox/yxjwk1py.default/

To load a custom user profile I ran through creating a profile in firefox and then did the following with the python selenium webdriver code:

def setUp(self):
    self.profile = webdriver.FirefoxProfile('/home/jmunsch/.mozilla/firefox/yxjwk1py.default')
    self.driver = webdriver.Firefox(self.profile)

System Info:

Python 2.7.3 (default, Sep 26 2013, 20:08:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources;pkg_resources.get_distribution("selenium").version

jmunsch@NE-522:~/Desktop/work$ firefox --version
Mozilla Firefox 26.0

also note

@Corey’s answer to manually set a profile

All of the configurables can be found under about:config:

profile.set_preference('browser.download.folderList', 2)

Answered By: jmunsch

You should add this:

profile.set_preference("browser.helperApps.neverAsk.openFile",
    "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml")

It does work!

Answered By: Gili Hary