1

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 Answers3

0

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

  • Even after options.add_experimental_option("detach", True) , the script still runs and after terminating the Script , the edge browser also gets closed – Ritesh Ghorpade Mar 22 '21 at 14:24
  • Instead of `add_experimental_option` can you try `set_capability`? (Found here: https://github.com/microsoft/edge-selenium-tools/blob/5baf710891e73ece02f5b58edff3895169366b90/py/msedge/selenium_tools/options.py#L74) – 森松水緒 Mar 24 '21 at 01:49
  • Added options.set_capability("detach", True) , but still did not work , I am running the python script using cmd , as soon as cmd gets closed , browser also closes – Ritesh Ghorpade Mar 24 '21 at 08:11
0

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()
chikabala
  • 653
  • 6
  • 24
0

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
Egret
  • 421
  • 1
  • 3
  • 13