Blaszard
Blaszard

Reputation: 31953

How can I stop Selenium Firefox from opening a URL in a new window? (instead of a tab)

I use Selenium on Python and tried to use Firefox driver (as Chrome is too slow), but for some reasons Firefox driver always opens a new URL in a separate window, not a tab. Here is my driver initialization:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2) 
options = webdriver.firefox.options.Options()
options.set_preference('intl.accept_languages', 'en-GB')
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_profile=profile, options=options)

And then it is just open each URL with window.open.

for url in page_lists:
    driver.execute_script('window.open("{0}", "_blank");'.format(url))

This opens each URL in a new tab in Chrome driver, but how can I make Firefox driver do the same thing?

I tried adding these preferences according to this documentation, but none of them worked. In fact, the browser does not have a check-tick on the "open in a new tab" on the preferences page.

profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_newwindow.restriction", 0)
profile.set_preference("browser.link.open_external", 3) 

profile.set_preference("browser.block.target_new_window", True)

Upvotes: 0

Views: 1128

Answers (1)

Michael Mintz
Michael Mintz

Reputation: 15496

On newer versions of selenium, you can use the following to open a new tab in the same window:

driver.switch_to.new_window("tab")

Then you would use the following to open a URL in that tab:

driver.get("URL")

Upvotes: 0

Related Questions