Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 388
ExperimentsWithCode Avatar asked Apr 11 '13 17:04

ExperimentsWithCode


People also ask

How to change default browser in Selenium?

We can open Chrome default profile with Selenium. To get the Chrome profile path, we need to input chrome://version/ in the Chrome browser and then press enter. We need to use the ChromeOptions class to open the default Chrome profile. We need to use the add_argument method to specify the path of the Chrome profile.

How do I use an existing Chrome profile in Selenium Python?

We can use a specific Chrome profile in Selenium. This can be done with the help of the ChromeOptions class. We need to create an object of this class and then apply addArguments method on it. The path of the specific Chrome profile that we want to use is passed as a parameter to this method.

Which is default browser for WebDriver?

Selenium comes with default Mozilla Firefox driver which is bundled in Selenium WebDriver jar file.

Which property should be set to run Firefox browser in Selenium?

Generally to run tests on our local machine, we will just specify as WebDriver driver = new FirefoxDriver(); to run on Firefox browser. System. setProperty("webdriver. gecko.


2 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:

  • http://docs.python.org/library/os.html

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)
like image 118
Corey Goldberg Avatar answered Oct 19 '22 08:10

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)

like image 17
jmunsch Avatar answered Oct 19 '22 10:10

jmunsch