[Python] Why does the driver open two tabs by one click?

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to reach pages by hrefs nested to web elements. But the driver gives me two identical tabs when it click() on the href. It turns out that three tabs are open at the same time. Duplicate pages confuse further work with html. How can I get only one new tab by click?

    Python 3.11, PyCharm

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome()
    driver.get("https://upn.ru/kupit/kvartiry")
    title = driver.title
    print(title)

    wait = WebDriverWait(driver, 5)
    original_window = driver.current_window_handle
    assert len(driver.window_handles) == 1

    b_block = driver.find_element(By.CLASS_NAME, 'main-container-margins.width-100').click()
    wait.until(EC.number_of_windows_to_be(3))
    for window_handle in driver.window_handles:
    if window_handle != original_window:
    driver.switch_to.window(window_handle)
    break
    title = driver.title
    print(title)


    [​IMG]

    Continue reading...

Compartilhe esta Página