2025, Nov 01 07:00
How to Stop Selenium from Opening Duplicate Tabs in Python + Chrome and Keep Window Handles Stable
Fix Selenium tests in Python and Chrome that open duplicate tabs. Learn why window.open duplicates occur and two safe fixes: navigate by href or remove target.
When you automate navigation across a site, a single click unexpectedly spawning multiple tabs is more than an annoyance. It breaks window-handle logic, scrambles the flow of assertions, and wastes time. In a Python 3.11 + Selenium + Chrome setup, clicking an anchor on a listings page led to three tabs: the original and two duplicates. The goal is straightforward: trigger only one new page change per interaction and keep window management consistent.
Reproducing the issue
The following snippet opens the target page, clicks an element, and waits for three windows. The outcome is exactly the problem: the click produces duplicate tabs, which then complicates any downstream handling of HTML and window focus.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("--headless")
browser = webdriver.Chrome() # options are not passed here on purpose
browser.get("https://upn.ru/kupit/kvartiry")
page_heading = browser.title
print(page_heading)
pause = WebDriverWait(browser, 5)
base_window = browser.current_window_handle
assert len(browser.window_handles) == 1
_ = browser.find_element(By.CLASS_NAME, 'main-container-margins.width-100').click()
pause.until(EC.number_of_windows_to_be(3))
for handle in browser.window_handles:
if handle != base_window:
browser.switch_to.window(handle)
break
page_heading = browser.title
print(page_heading)
What actually happens
The page responds to the interaction twice. First, the normal anchor behavior fires and Chrome opens a new tab. Then a JavaScript listener attached to the same element calls window.open(), producing a second copy. WebDriver merely drives the browser; if the page instructs it to open the target twice, you’ll see two new tabs.
Two safe ways to proceed
The reliable fix is to stop triggering the duplicate behavior. You can either navigate directly to the URL without clicking at all, or neutralize the extra handler and then click once.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
cfg = webdriver.ChromeOptions()
cfg.add_argument('--headless')
drv = webdriver.Chrome(options=cfg)
drv.get('https://upn.ru/kupit/kvartiry')
waiter = WebDriverWait(drv, 5)
anchor = waiter.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, 'a.card-title') # adjust this selector for your target
))
# Approach 1: skip the click and open the destination yourself
href_value = anchor.get_attribute('href')
drv.get(href_value)
# --- Alternatively, if you really want to "click" ---
# drv.execute_script(
# "arguments[0].removeAttribute('target'); arguments[0].click();", anchor)
# waiter.until(EC.number_of_windows_to_be(2))
In the first approach you extract href and navigate to it directly. No new tabs are created unexpectedly, and the browser stays in a single window. In the second approach you still click, but you remove the target attribute and wait for exactly two windows, which keeps the number of tabs predictable.
Why this matters
Stable window management is essential for robust automation. When each interaction deterministically yields one page change, your handle switching, assertions, and DOM queries stay aligned. Eliminating accidental duplicates prevents your test flow from diverging and keeps the code simple.
Practical notes
Selector accuracy is important. Once you lock onto the correct element with a proper CSS selector, direct navigation by href works cleanly. If the selector is off, you may run into messages like InvalidArgumentException: 'url' must be a string. Ensuring that you resolve the right link and pass its href to driver.get avoids that pitfall.
Wrap-up
Duplicate tabs are typically a product of the page triggering two navigation paths at once. Treat the root cause by avoiding the click altogether and navigating via href, or by neutralizing the extra handler before clicking. Keep the flow to one deterministic page open, and your window-handle logic remains consistent and easy to reason about.