Selenium Python Firefox webdriver: unable to change profile
I want to use the "new tab instead of window" option for the Firefox web browser. 1 / I created a profile with this option, but when I use the profile, many parameters are ok, but not this one. 2 / After loading the profile, I tried to change the parameter in the code, but it doesn'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())
Everything is ok (start page is google.fr) except for this option, which is not ok.
It seems that Selenium is copying the profile to a temporary directory. 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
source to share
From what I have researched browser.link.open_newwindow
is the frozen setting and always syncs with the value 2
. If you dig up the selenium Python bindings source you will find a set of optionsfrozen
to be applied after your custom settings are set.
Note that in java
bindings this set of frozen defaults 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 little explanation coming from Firefox only supports non-tabbed windows :
This is a known issue and unfortunately we will not support tabs.
We force Firefox to open all links in a new window. We cannot access the tabs to know when to switch. When we go to the puppet (Mozilla project) in the future we will be able to do this, but for now it is working as intended
A workaround would be to target
manually change the links - may not work in all cases depending on how the new link is opened.
source to share