Search code examples
seleniumgoogle-chromeselenium-webdriverbrowserautomation

Browser closes even after using detach in selenium


My problem I have used detach method to stop the browser to close automatically after the code run. Its not working. Below I have given the code for your reference. Please help me. i have tried implicit wait also but its not working thanks

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
s = Service(ChromeDriverManager().install())
# chrome_options = Options()
# chrome_options.add_experimental_option("detach",True)
"""
The browser will be started.
"""
browser = webdriver.Chrome(service=s)
browser.maximize_window()
chrome_options = Options()
chrome_options.add_experimental_option("detach",True)
browser.get("https://rahulshettyacademy.com")
print(browser.title)
browser.get("https://rahulshettyacademy.com/angularpractice/")
print(browser.title)
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[1]/input[1]").send_keys("OVM")
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[2]/input[1]").send_keys("ovm@gmail.com")
browser.find_element(By.ID,"exampleInputPassword1").send_keys("123456789")
browser.find_element(By.XPATH,"//input[@id='exampleCheck1']").click()
browser.find_element(By.XPATH,"//select[@id='exampleFormControlSelect1']").click()
browser.find_element(By.XPATH,"//input[@id='inlineRadio2']").click()
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[7]/input[1]").send_keys("01/01/2000")
browser.implicitly_wait(20000)

Result:-

Rahul Shetty Academy
ProtoCommerce

Process finished with exit code 0

the browser closes automatically. I did not give any comment to close the browser.


Solution

  • You've not set the detach option correctly. The following code works for me:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from webdriver_manager.chrome import ChromeDriverManager
    s = Service(ChromeDriverManager().install())
    chrome_options = Options()
    chrome_options.add_experimental_option("detach",True)
    """
    The browser will be started.
    """
    browser = webdriver.Chrome(service=s, options=chrome_options)
    browser.maximize_window()
    browser.get("https://rahulshettyacademy.com")
    print(browser.title)
    browser.get("https://rahulshettyacademy.com/angularpractice/")
    print(browser.title)
    browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[1]/input[1]").send_keys("OVM")
    browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[2]/input[1]").send_keys("ovm@gmail.com")
    browser.find_element(By.ID,"exampleInputPassword1").send_keys("123456789")
    browser.find_element(By.XPATH,"//input[@id='exampleCheck1']").click()
    browser.find_element(By.XPATH,"//select[@id='exampleFormControlSelect1']").click()
    browser.find_element(By.XPATH,"//input[@id='inlineRadio2']").click()
    browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[7]/input[1]").send_keys("01/01/2000")
    browser.implicitly_wait(20000)