Selenium : How to stop the python script after automation is complete ,and still use the EDGE browser normally after automation

Viewed 574

So I am doing an automation task , where I login to website on new Chromium Edge and go to particular pages , once it is done , I want to use the Edge browser normally on that website itself, but saw that python script keeps running , and once I close the script the browser also closes

So what I want is that , once automation is complete , python script should end , and I should be able to use the Edge browser from that point normally , from the current page itself

I am using Edge as Browser like below add experimental option detach for Edge does not work

from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = EdgeOptions()
options.use_chromium = True
options.add_experimental_option("detach", True)
driver = Edge(options=options)
driver.get("mywebsite")
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(
        (By.XPATH, "/html/body/header/nav/div/div[2]/a"))
)
element.click()
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "username"))
)
element.send_keys("username")
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "password"))
)
element.send_keys("password")
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "continue"))
)
element.click()

Note : I am running the python script using cmd , and if close the cmd , no matter what the browser also closes

3 Answers

Before starting the webdriver, specify the detach option to True.

# example with chrome driver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

related answer

you can simply close the driver at the end of the file using this :

driver.close()

To end the script only not the browser add this :

quit()

I'm not sure if the edge webdriver has added any option to achieve what you need. For now you can use two workarounds -

  1. Add time.sleep(10)
  2. (If you decide to not use cmd) you can set a breakpoint before debugging in your IDE
Related