I want to open all links in the same window instead in new window.
I tried
profile.setPreference("browser.link.open_newwindow", 1)
but the result is:
WARNING: traffic.loop 0 error: Preference browser.link.open_external may not be overridden: frozen value=2, requested value=1
Is there an another way to open the links in the same window ?
I've found a workaround!
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "document.getElementById('yourFormOrAnchorId').target=''";
js.executeScript(script);
After that you can select your anchor or any of the form elements and click or submit it. The target page will open in the same tab.
This basically changes the current HTML page so that anchors and forms don't force the browser to open new tabs or windows. For testing this might be suboptimal, but it simplifies the writing of tests a lot.
Try this out...
Modify FireFox profile parameters "browser.link.open_newwindow.restriction" and "browser.link.open_newwindow".
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.link.open_newwindow.restriction", 0);
profile.setPreference("browser.link.open_newwindow", 1);
If you are using Google Chrome then simply install this extension and it will take care of the rest of the task. This extension is also handy to open pop-ups in new tabs which usually opens in new windows. (First you need to download the extension .crx file from given location.)
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
You should modify the firefox profile parameters:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_newwindow.restriction", 0)
driver = webdriver.Firefox(firefox_profile=profile)
if this methode does not work, you can set perference using firefox Options:
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_preference("browser.link.open_newwindow.restriction", 0)
opts.set_preference("browser.link.open_newwindow", 3)
driver = webdriver.Firefox(firefox_options=opts)
(A) browser.link.open_newwindow - for links in Firefox tabs :
3 : divert new window to a new tab (default)
2 : allow link to open a new window
1 : force new window into same tab
(B) browser.link.open_newwindow.restriction - for links in Firefox tabs
0 : apply the setting under (A) to ALL new windows (even script windows)
2 : apply the setting under (A) to normal windows, but NOT to script windows
with features (default)
1 : override the setting under (A) and always use new windows
According to Selium docs (https://code.google.com/p/selenium/wiki/FirefoxDriver) the following property webdriver.firefox.profile controls the firefox profile used.
Which is where firefox gets the browser.link.open_newwindow on start up from. To create a new profile for your tests you can follow the instructions here https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles detailed configuration of the profile can be done either by editing the profile's pref.js or firing up the profile and editing it via about:config.
hope this of help!
In the selenium config file:
C:\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver_prefs.json
change the following line from:
"browser.link.open_newwindow": 2,
to:
"browser.link.open_newwindow": 3,
I test it and it worked
Actually, Selenium is not responsible of the page opens in a new window or in a same window. It is fully depends upon the Browser settings which you used for execution.
For a sake take Firefox browser
If you want to open all the links in a new window. Do these steps
- Open Tools
- Click Options
- Click Tabs menu
- Check the box of
Open new windows in a new tab instead.
Now click the link which opens a window. It will opens in a new tab of same window.