Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Python Firefox webdriver : can't modify profile

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

like image 745
philnext Avatar asked Dec 12 '25 23:12

philnext


1 Answers

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.

like image 178
alecxe Avatar answered Dec 15 '25 13:12

alecxe