1

I am trying to open multiple tabs within the same browser window in Selenium. I am unable to open multiple tabs if I use a firefox profile. The tabs open normally without the profile. I have searched a lot , but the available answers are opening the tabs as separate windows. What I am after is multiple tabs in the same window in Firefox using a Firefox Profile.

System Information:

Windows7
Python 3.7
Firefox 84
Selenium 3.141

I have created a test firefox profile.

Code with Firefox Profile that doesn't work - tabs are opened as separate windows.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.keys import Keys

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
fp = webdriver.FirefoxProfile('C:\\Users\\john\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\0kjv3jas.test')
fp.update_preferences()
first_link = "https://google.com"
second_link = "https://reddit.com"
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_binary=binary, firefox_profile=fp, executable_path='C:\\WebDriver\\bin\\geckodriver.exe')
driver.get(first_link)
driver.execute_script("window.open('" + second_link +"');")

Code without the Firefox profile works - create the tabs normally

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.keys import Keys

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
first_link = "https://google.com"
second_link = "https://reddit.com"
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_binary=binary, executable_path='C:\\WebDriver\\bin\\geckodriver.exe')
driver.get(first_link)
driver.execute_script("window.open('" + second_link +"');")

References:

Open web in new tab Selenium + Python
Selenium multiple tabs at once
Python -- Opening multiple tabs using Selenium
Open multiple tabs in selenium using python
Open multiple tabs in selenium using python
Selenium Switch Tabs

https://gist.github.com/lrhache/7686903
https://www.lambdatest.com/blog/python-selenium-switch-tabs/

depar
  • 49
  • 1
  • 7

1 Answers1

3

Remove firefox profile and it works fine , you are calling an empty profile so you don't need that

driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_binary=binary,  options=options, executable_path='C:\\WebDriver\\bin\\geckodriver.exe')

if you want to use it with profile then use :

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
fp = webdriver.FirefoxProfile()

fp.DEFAULT_PREFERENCES["frozen"]["browser.link.open_newwindow"] = 3
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Thnx, its working after removing the empty profile. If I add a profile, (I have updated the code) then its back to suare one, the tabs open as new window. – depar Jan 25 '21 at 01:20
  • 1
    use fp.DEFAULT_PREFERENCES["frozen"]["browser.link.open_newwindow"] = 3 – PDHide Jan 25 '21 at 12:29
  • 1
    awesome that was magic. I had changed the `browser.link.open_newwindow` to 3 in `about:config` but never thought that Selenium copies the profile to a temp `rust`. Thanks for pointing this out, that `browser.link.open_newwindow` is a frozen preference, which means it can't be modified using `profile.set_preference("browser.link.open_newwindow", 3)` – depar Jan 25 '21 at 12:55