3

I want to use, on a webdriver Firefox instance, the "new tab instead of window" option. 1/ I created a profile with this option on, but when I use the profile a lot of options are OK but not this one. 2/ After the load of the profile I tried to change the option in the code but it does n't work. My code :

profile = webdriver.FirefoxProfile(os.path.join(s_path, name))
profile.set_preference("browser.link.open_newwindow.restriction", 0)
profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_external", 3)
profile.set_preference("browser.startup.homepage","http://www.google.fr")
profile.update_preferences()
print(os.path.join(s_path, name))
driver = webdriver.Firefox(set_profile())

All is OK (the start homepage is google.fr) except this option which is not OK.

It seems that Selenium copy the profile in a temp dir. where users.js have the wrong line :

user_pref("browser.link.open_newwindow", 2);

Python 3.4.2, Windows 7, Firefox 39.0, Selenium lib 2.46

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
philnext
  • 3,242
  • 5
  • 39
  • 62

2 Answers2

4

From what I've researched, browser.link.open_newwindow is a frozen setting and it's always synced with the value 2. If you dig up the source of the selenium Python bindings, you would find a set of frozen settings that is applied after your custom settings are set.

Note that in java bindings this set of default frozen settings is explicitly hardcoded:

  /**
   * Profile preferences that are essential to the FirefoxDriver operating correctly. Users are not
   * permitted to override these values.
   */
  private static final ImmutableMap<String, Object> FROZEN_PREFERENCES =
      ImmutableMap.<String, Object>builder()
          .put("app.update.auto", false)
          .put("app.update.enabled", false)
          .put("browser.download.manager.showWhenStarting", false)
          .put("browser.EULA.override", true)
          .put("browser.EULA.3.accepted", true)
          .put("browser.link.open_external", 2)
          .put("browser.link.open_newwindow", 2)  // here it is
          // ...

And a bit of an explanation coming from Firefox only supports windows not tabs:

This is a known issue and unfortunately we will not be supporting tabs.

We force Firefox to open all links in a new window. We can't access the tabs to know when to switch. When we move to marionette (Mozilla project) in the future we should be able to do this but for now it is working as intended

A workaround solution would be to change the target of a link manually - may not work in all of the cases depending on how a new link is opened.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Nice answer, for my own use, I prefered to change the json file : \Python34\Lib\site-packages\selenium\webdriver\firefox\webdriver_prefs.json where the frozen lines are defined... – philnext Jul 13 '15 at 14:40
  • @philnext oh yeah, nice idea! Thanks for sharing! – alecxe Jul 13 '15 at 14:41
3

"browser.link.open_newwindow" is a frozen preference, which means that it can't be modified using profile.set_preference("browser.link.open_newwindow", 3)

The solution is to use profile.DEFAULT_PREFERENCES["frozen"]["browser.link.open_newwindow"] = 3 instead. (the other non-frozen preferences can be set with set_preference without problem)

John
  • 31
  • 3