Your ultimate guide

Fix Browser Closes Automatically - Immediately after test without Calling Quit and Close()

To resolve issues with automatically closing browsers while using Selenium in Python, it is necessary to properly configure the Chrome WebDriver:

To configure Chrome WebDriver pre-requirements:
  1. Before running the script, make sure you have the required Python packages installed. You can install them using pip if you haven't already:
    • pip install selenium webdriver-manager
  2. These commands will install the 'selenium' package for Selenium WebDriver and the 'webdriver-manager' package for managing WebDriver executables like ChromDriver automatically.
  3. To install packages using pip within PyCharm, you can follow these steps:
    1. Open PyCharm: Launch Pycharm and open your Python project or create a new one.
    2. Open a Terminal: In PyCharm, you can access a terminal by clicking on "View" in the menu bar and selecting "Tool Windows" and then "Terminal".
    3. Run the pip Command: In the terminal, run the following command.
      • pip install webdriver-manager
    4. Wait for Installation: Pip will download and install the package and its dependencies.



Here's how you can fix this issue:
  1. Import the necessary modules
    • from selenium.webdriver.chrome.service import Service
    • from webdriver_manager.chrome import ChromeDriverManager
  2. Creates an instance of the 'ChromeOptions' class, which allows you to configure various options and preferences for the Chrome WebDriver.
    • options = webdriver.ChromeOptions()
  3. Add an experimental option to the 'ChromeOptions' object and pass the "detach" option, set to True, which allows the WebDriver to detach from the browser session after the WebDriver script has finished executing. This means that the browser window will remain open even after your script has completed its tasks.
    • options.add_experimental_option("detach", True)
  4. Create a WebDriver instance for Chrome with the specified options. It uses the ChromeDriverManager to automatically download and manage the Chrome WebDriver executable (chromedriver.exe) and set it up for use with Selenium. 
    • driver = webdriver.Chrome(options = options, service = Service(ChromeDirverManager().install()))
  5. With this configuration, you can now use the 'driver' object to interact with the Chrome browser, and the browser window will remain open even after your Selenium script has been completed.
  6. If you call the quit or close() function, then the browser will close.



Example Programs:
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By

  3. from selenium.webdriver.chrome.service import Service
  4. from webdriver_manager.chrome import ChromeDriverManager

  5. options = webdriver.ChromeOptions()
  6. options.add_experimental_option("detach", True)
  7. driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))

  8. driver.get("https://example.com")


No comments:

Post a Comment