Bird
Raised Fist0
Selenium Pythontesting~8 mins

Closing browser (close vs quit) in Selenium Python - Framework Approaches Compared

Choose your learning style10 modes available
Framework Mode - Closing browser (close vs quit)
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   └── test_checkout.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── checkout_page.py
├── utils/
│   ├── browser_manager.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── test_report.html
└── conftest.py
Test Framework Layers
  • Driver Layer: Manages browser sessions using Selenium WebDriver. Handles opening, closing, and quitting browsers.
  • Page Objects: Classes representing web pages. They use the driver to interact with page elements.
  • Tests: Test scripts that use page objects and driver layer to perform test scenarios.
  • Utilities: Helper functions and browser management code, including methods to close or quit browsers properly.
  • Configuration: Stores environment settings, browser types, and credentials for flexible test runs.
Configuration Patterns
  • Environment Settings: Use config/config.yaml to define URLs, browser types (e.g., Chrome, Firefox), and timeouts.
  • Browser Choice: Parameterize browser selection in browser_manager.py to support multiple browsers.
  • Credentials: Store securely in credentials.yaml and load them in tests or fixtures.
  • Session Management: Use fixtures in conftest.py to initialize and clean up browser sessions, deciding when to call close() or quit().
Test Reporting and CI/CD Integration
  • Generate HTML reports in reports/ folder using pytest-html or similar plugins.
  • Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on code commits.
  • Ensure browser sessions are properly closed or quit after tests to avoid resource leaks in CI environments.
  • Use logs to capture browser open/close events for debugging test failures.
Framework Design Principles
  1. Use driver.close() to close the current browser window only. This is useful when multiple windows or tabs are open and you want to close one without ending the session.
  2. Use driver.quit() to end the entire browser session. This closes all windows and safely ends the WebDriver session, freeing resources.
  3. Always call quit() in test teardown or fixture finalizers. This prevents orphan browser processes and memory leaks.
  4. Manage browser lifecycle centrally in utilities or fixtures. Avoid calling close or quit randomly in tests to keep tests clean and maintainable.
  5. Document the difference clearly for team members. Helps avoid confusion and bugs related to browser session handling.
Self Check

Where in this framework structure would you add a method to safely close the current browser window without quitting the entire session?

Key Result
Use driver.close() to close one window and driver.quit() to end the full browser session, managing them centrally in utilities or fixtures.

Practice

(1/5)
1. What does the close() method do in Selenium WebDriver?
easy
A. Closes all browser windows and ends the WebDriver session
B. Restarts the browser session
C. Keeps the browser open but clears cookies
D. Closes the current browser window or tab only

Solution

  1. Step 1: Understand the purpose of close()

    The close() method is designed to close only the current browser window or tab that the WebDriver is controlling.
  2. Step 2: Compare with quit()

    Unlike quit(), which closes all windows and ends the session, close() affects only one window or tab.
  3. Final Answer:

    Closes the current browser window or tab only -> Option D
  4. Quick Check:

    close() closes one window [OK]
Hint: Close closes one tab/window; quit closes all [OK]
Common Mistakes:
  • Confusing close() with quit()
  • Thinking close() ends the WebDriver session
  • Assuming close() clears cookies
2. Which of the following is the correct syntax to close all browser windows and end the WebDriver session in Selenium Python?
easy
A. driver.quit()
B. driver.stop()
C. driver.exit()
D. driver.close()

Solution

  1. Step 1: Identify the method to end WebDriver session

    The method quit() is used to close all browser windows and end the WebDriver session properly.
  2. Step 2: Verify syntax correctness

    driver.quit() is the correct syntax; other options like exit() or stop() do not exist in Selenium WebDriver API.
  3. Final Answer:

    driver.quit() -> Option A
  4. Quick Check:

    Use quit() to close all windows [OK]
Hint: Use driver.quit() to close all windows and end session [OK]
Common Mistakes:
  • Using driver.close() to end session
  • Using non-existent methods like exit() or stop()
  • Missing parentheses in method call
3. Consider the following Selenium Python code snippet:
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
driver.close()
print(driver.title)

What will happen when this code runs?
medium
A. Prints the page title successfully
B. Raises an error because the browser window is closed before accessing title
C. Prints an empty string
D. The browser stays open and prints the title

Solution

  1. Step 1: Analyze the effect of driver.close()

    The close() method closes the current browser window, so after this call, the window is no longer available.
  2. Step 2: Accessing driver.title after closing window

    Trying to get the title after closing the window causes a WebDriverException because the session still exists but the window is closed.
  3. Final Answer:

    Raises an error because the browser window is closed before accessing title -> Option B
  4. Quick Check:

    Accessing title after close() causes error [OK]
Hint: Don't access page after close() without reopening [OK]
Common Mistakes:
  • Assuming title prints after close()
  • Thinking close() ends session (it doesn't)
  • Ignoring exceptions after window close
4. You have this Selenium Python code:
driver = webdriver.Firefox()
driver.get('https://example.com')
driver.close()
driver.quit()

What is the problem with this code?
medium
A. Calling close() before quit() is redundant but safe
B. close() should be replaced with quit() only
C. Calling quit() after close() causes an error
D. The code will open two browser windows

Solution

  1. Step 1: Understand the effect of close() and quit()

    close() closes the current window; quit() closes all windows and ends the session.
  2. Step 2: Analyze calling close() before quit()

    Calling close() first closes one window, then quit() closes any remaining windows and ends the session. This is redundant but does not cause errors.
  3. Final Answer:

    Calling close() before quit() is redundant but safe -> Option A
  4. Quick Check:

    close() then quit() is safe but redundant [OK]
Hint: quit() alone suffices; close() before quit() is safe but extra [OK]
Common Mistakes:
  • Thinking quit() after close() causes error
  • Believing close() replaces quit()
  • Expecting multiple windows opened
5. You want to write a Selenium test that opens two tabs, performs actions on both, then closes only the second tab while keeping the first tab open. Which method should you use to close the second tab without ending the WebDriver session?
hard
A. driver.stop()
B. driver.quit()
C. driver.close()
D. driver.exit()

Solution

  1. Step 1: Understand the requirement to close only one tab

    To close only the second tab and keep the first open, you must close the current window or tab without ending the session.
  2. Step 2: Choose the correct method

    close() closes the current window or tab only, while quit() closes all windows and ends the session. Methods stop() and exit() do not exist in Selenium.
  3. Final Answer:

    driver.close() -> Option C
  4. Quick Check:

    Use close() to close one tab, quit() closes all [OK]
Hint: Use close() to close one tab, quit() closes all tabs [OK]
Common Mistakes:
  • Using quit() which closes all tabs
  • Using non-existent methods like stop() or exit()
  • Confusing close() with quit()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard