Home > Back-end >  How to close multiple selenium sessions by user`s wish?
How to close multiple selenium sessions by user`s wish?

Time:10-04

I launch multiple selenium sessions with multiprocessing and want to close them all or a few when user wants it

schema of my code:

from multiprocessing import Process

def drop():
    ...
    driver = webdriver.Chrome(executable_path=os.path.join(path, '\\chromedriver.exe'),chrome_options=options)
    ...

for i in range(10):
     s=Process(target = drop)
     s.start()
input('press enter to stop all sessions')
###close selenium driver event

CodePudding user response:

you can create an array process_list = [] and append the 's' variable in the list for every iteration process_list.append(s) and then to stop all processes

for i in process_list:
  driver.close()
  i.terminate()

CodePudding user response:

Adding each driver instance to a list when creating them and then iterating through them one by one to close them afterwards might be able to solve your issue.

from multiprocessing import Process

list_drivers = []

def drop():
    ...
    driver = webdriver.Chrome(executable_path=os.path.join(path, '\\chromedriver.exe'),chrome_options=options)
    list_drivers.append(driver)
    ...

for i in range(10):
     s=Process(target = drop)
     s.start()

input('press enter to stop all sessions')

for driver in list_drivers:
  driver.quit()

###close selenium driver event
  • Related