Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Python Selenium Firefox: Unable to open multiple tabs when using Firefox Profiles

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/

question from:https://stackoverflow.com/questions/65877506/python-selenium-firefox-unable-to-open-multiple-tabs-when-using-firefox-profile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...