Skip to content

await driver.close() closes the entire window instead of current Ta #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bizjaya opened this issue Feb 2, 2025 · 1 comment
Open
Labels
invalid This doesn't seem right

Comments

@bizjaya
Copy link

bizjaya commented Feb 2, 2025

I expected await driver.close() to close only the current tab, but it closes the entire browser window instead.

Steps to Reproduce
Open multiple tabs using execute_script("window.open(...);").
Switch to the new tab.
Call await driver.close().
Instead of closing only the new tab, the entire browser session terminates.
Expected Behavior
await driver.close() should close only the currently active tab.
The browser window should remain open with other tabs intact.
Actual Behavior
The entire browser window closes instead of just the active tab.

`
for idx, h2 in enumerate(job_titles, start=1):
try:
# Find inside


link = await h2.find_element(By.CSS_SELECTOR, "a")
job_link = await link.get_attribute("href")
logging.info(f"Opening job {idx}: {job_link}")

    # Open new tab for the job link
    await driver.execute_script(f"window.open('{job_link}', '_blank');")
    await asyncio.sleep(1)  # Short delay for tab update

    # Get the current tab handles
    all_tabs = await driver.window_handles

    # Ensure there's more than one tab
    if len(all_tabs) <= 1:
        logging.warning("No new tab detected, skipping...")
        continue

    # Identify the new tab (the last handle in the list)
    new_tab = all_tabs[-1]

    # Switch to new tab
    await driver.switch_to.window(new_tab)

    # Wait for page to load
    try:
        await driver.find_element(By.TAG_NAME, 'body')
        logging.info("Page loaded successfully")
    except Exception as e:
        logging.error(f"Error waiting for page load: {e}")

    await asyncio.sleep(3)  # Let content load

    # Print page source before closing tab
    page_source = await driver.page_source
    logging.info(f"Page Source for job {idx}:\n{page_source[:100]}")  # First 100 chars

    # Close the new tab only if there are multiple tabs open
    remaining_tabs = await driver.window_handles
    if len(remaining_tabs) > 1:
        await driver.close()  # Close the current tab
        logging.info("New tab closed successfully")

        # Switch back to main tab
        remaining_tabs = await driver.window_handles
        if main_tab in remaining_tabs:
            await driver.switch_to.window(main_tab)
        else:
            logging.error("Main tab closed unexpectedly!")
    else:
        logging.warning("Skipping tab close: Only one tab left!")

except Exception as e:
    logging.error(f"Error processing job {idx}: {e}")

`

@kaliiiiiiiiii
Copy link
Owner

@bizjaya please refactor the formatting in this issue. Checkout how this works in the github docs.

@kaliiiiiiiiii kaliiiiiiiiii added the invalid This doesn't seem right label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants