Selenium2 firefox: use the default profile

18,149

Solution 1

Simon Stewart answered this on the mailing list for me.

To summarize his reply: you take your firefox profile, zip it up (zip, not tgz), base64-encode it, then send the whole thing as a /session json request (put the base64 string in the firefox_profile key of the Capabilities object).

An example way to do this on Linux:

cd /your/profile
zip -r profile *
base64 profile.zip > profile.zip.b64

And then if you're using PHPWebDriver when connecting do:

$webdriver->connect("firefox", "", array("firefox_profile" => file_get_contents("/your/profile/profile.zip.b64")))

NOTE: It still won't be my real profile, rather a copy of it. So bookmarks won't be remembered, the cache won't be filled, etc.

Solution 2

Here is the Java equivalent. I am sure there is something similar available in php.

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
WebDriver driver = new FirefoxDriver(ffprofile);

If you want to additonal extensions you can do something like this as well.

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
ffprofile.addExtension(new File("path/to/my/firebug.xpi"));
WebDriver driver = new FirefoxDriver(ffprofile);

Solution 3

java -jar selenium-server-standalone-2.21.0.jar -Dwebdriver.firefox.profile=default

should work. the bug is fixed.

Just update your selenium-server.

Solution 4

I was curious about this as well and what I got to work was very simple. I use the command /Applications/Firefox.app/Contents/MacOS/firefox-bin -P to bring up Profile Manager. After I found which profile I needed to use I used the following code to activate the profile browser = Selenium::WebDriver.for :firefox, :profile => "batman".

This pulled all of my bookmarks and plug-ins that were associated with that profile.

Hope this helps.

Solution 5

From my understanding, it is not possible to use the -Dwebdriver.firefox.profile=<name> command line parameter since it will not be taken into account in your use case because of the current code design. Since I faced the same issue and did not want to upload a profile directory every time a new session is created, I've implemented this patch that introduces a new firefox_profile_name parameter that can be used in the JSON capabilities to target a specific Firefox profile on the remote server. Hope this helps.

Share:
18,149
Darren Cook
Author by

Darren Cook

I'm data scientist, software developer, computer book author, entrepreneur. I'm director at QQ Trends, a company that solves difficult data and software challenges for our clients. Lots of machine learning, especially NLP-related, recently. (We sometimes have freelance projects, so get in touch if interested.) (Contact me at dc at qqtrend dot com: please mention you are coming from StackOverflow, so I know it is not spam.) My first book was "Data Push Apps with HTML5 SSE", with O'Reilly, 2014 (ISBN: 978-1449371937). Old by computer standards, but the standard has been stable, so surprisingly still useful. My second book, at the end of 2016, also with O'Reilly, was Practical Machine Learning with H2O (ISBN: 978-1491964606). I'm British, speak English and Japanese (fairly fluent, 1 kyu), with a bit of German, Chinese and Arabic. As for computer languages, I've done commercial work in most of them; but it has been mostly JavaScript, R, Python, C++ the past five years. All my Stack Overflow and all my Stack Exchange contributions (across all sites) are dedicated to the public domain or available under the CC0 license at your choice. I don't like viral licenses. Easy ways to irritate me on StackExchange sites (whether my own question or someone else's): 1. Downvote without a comment (N/A if someone already left a comment and you just agree with it, of course); 2. Answers in comments. Other than that I'm an easy-going and pragmatic guy :-)

Updated on June 09, 2022

Comments

  • Darren Cook
    Darren Cook almost 2 years

    Selenium2, by default, starts firefox with a fresh profile. I like that for a default, but for some good reasons (access to my bookmarks, saved passwords, use my add-ons, etc.) I want to start with my default profile.

    There is supposed to be a property controlling this but I think the docs are out of sync with the source, because as far as I can tell webdriver.firefox.bin is the only one that works. E.g. starting selenium with:

    java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin=not-there
    

    works (i.e. it complains). But this has no effect:

    java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.profile=default
    

    ("default" is the name in profiles.ini, but I've also tried with "Profile0" which is the name of the section in profiles.ini).

    I'm using PHPWebdriver (which uses JsonWireProtocol) to access:

    $webdriver = new WebDriver("localhost", "4444");
    
    $webdriver->connect("firefox");
    

    I tried doing it from the PHP side:

    $webdriver->connect("firefox","",array('profile'=>'default') );
    

    or:

    $webdriver->connect("firefox","",array('profile'=>'Profile0') );
    

    with no success (firefox starts, but not using my profile).

    I also tried the hacker's approach of creating a batch file:

    #!/bin/bash
    /usr/bin/firefox -P default
    

    And then starting Selenium with: java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin="/usr/local/src/selenium/myfirefox"

    Firefox starts, but not using by default profile and, worse, everything hangs: selenium does not seem able to communicate with firefox when started this way.

    P.S. I saw Selenium - Custom Firefox profile I tried this:

    java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate "not-there"
    

    And it refuses to run! Excited, thinking I might be on to something, I tried:

    java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate /path/to/0abczyxw.default/
    

    This does nothing. I.e. it still starts with a new profile :-(

  • Darren Cook
    Darren Cook over 12 years
    Thanks @nilesh. Am I correct in thinking this is using WebDriver directly; you are not starting selenium-server-standalone.jar and therefore not using JsonWireProtocol?
  • nilesh
    nilesh over 12 years
    This is using webdriver directly. I am not sure what you mean by starting with stand alone jar. WebDriver itself uses JsonWireProtocol to communicate code.google.com/p/selenium/wiki/JsonWireProtocol
  • Darren Cook
    Darren Cook over 12 years
    Ah, interesting. So your above java snippet somehow gets turned into JSON and web service calls? If I could just track down what those calls are then I could implement it in the PHP library. But whatever it is doing is not documented at the URL you gave, and I've not see any other docs for the JsonWireProtocol. I think I'll contact the Selenium developers.
  • Curtis Miller
    Curtis Miller over 12 years
    @Darren: the language is in ruby. But I am sure there is an equivalent for PHP.
  • Darren Cook
    Darren Cook over 12 years
    Great @Stéphane - if I've understood your patch, that is exactly what I was after. I've not tested it yet, but is it safe? (i.e. in its tidyup stage selenium deletes the temporary profile directory it creates; don't you need explicit code to stop that happening?)
  • David
    David over 12 years
    Thanks, that was really helpful. Would be nice if the PHP code to zip the profile and then encode to base64 was provided so one could do everything in PHP.
  • anonymous-one
    anonymous-one almost 12 years
    Reconfirming what has already been stated... This is the way to go. Worked flawless for us when we wanted to pipe all selenium requests thru a proxy.
  • Darren Cook
    Darren Cook almost 12 years
    Great, thanks Muhammad. I just confirmed that does now work. (BTW, it is still a copy of the profile; i.e. cache/history are not filled in.)
  • Muhammad Umer Farooq
    Muhammad Umer Farooq almost 12 years
    oh, I just use to save ssl certificates and some properties like dom.max_script_run_time.
  • Stéphane
    Stéphane over 11 years
    Yes, it is safe. Selenium will in fact make a temporary copy of this profile and use it (like a template).