Selenium2 Firefox: Use the Default Profile

Using the default firefox profile with selenium webdriver in python

Ok, I just solved this by simply changing all the slashes in my file path from "\" to "/".
Never knew this would make a difference.

C:/Users/admin/AppData/Roaming/Mozilla/Firefox/Profiles/c1r3g2wi.default

Selenium2 firefox: use the default profile

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.

How can I set a default profile for the Firefox driver in Selenium Webdriver 3?

As you are using Selenium 3.14.0 as per the FirefoxDriver Class the valid constructors are:

  • FirefoxDriver()
  • FirefoxDriver(FirefoxOptions options)
  • FirefoxDriver(GeckoDriverService service)
  • FirefoxDriver(GeckoDriverService service, FirefoxOptions options)
  • FirefoxDriver(XpiDriverService service)
  • FirefoxDriver(XpiDriverService service, FirefoxOptions options)

So, as per your code attempts the following is not a valid option to invoke FirefoxDriver()

WebDriver driver = new FirefoxDriver(profile);

Solution

To invoke invoke FirefoxDriver() with the default profile you need to use the setProfile(profile) method to set the FirefoxProfile through an instance of FirefoxOptions() and you can use the following code block:

  • Code Block:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.firefox.ProfilesIni;
    import org.testng.annotations.Test;

    public class A_FirefoxProfile {

    @Test
    public void seleniumFirefox() {
    System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");
    ProfilesIni profileIni = new ProfilesIni();
    FirefoxProfile profile = profileIni.getProfile("default");
    FirefoxOptions options = new FirefoxOptions();
    options.setProfile(profile);
    WebDriver driver = new FirefoxDriver(options);
    driver.get("http://www.google.com");
    System.out.println(driver.getTitle());
    }

    }
  • Console Output:

    [RemoteTestNG] detected TestNG version 6.14.2
    1537775040906 geckodriver INFO geckodriver 0.20.1
    1537775040923 geckodriver INFO Listening on 127.0.0.1:28133
    Sep 24, 2018 1:14:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Google
    PASSED: seleniumFirefox

    ===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
    ===============================================

    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================

What profile does Selenium WebDriver use by default?

I will answer it, supporting comment from @twall: When starting firefox in Selenium 2 WebDriver, it starts new, anonymous profile.

However, if you want to change it, you can create new Firefox profile and name it somehow, you know what it is - e.g. SELENIUM

Then in your code do this:

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

That way, Firefox will always start that profile. In the profile you do all the settings you need

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

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)


Related Topics



Leave a reply



Submit