1

I'm pretty new in programming so I might be an easy question but I don't understand why the browsers opened by Selenium closes at the end of the code :

def login(Email, Password):
    Driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe",options=chrome_options);  
    Driver.maximize_window()
    Driver.get("https://www.ABC.com")
    time.sleep(0.5)
    search = Driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button")
    search.click()
    time.sleep(0.5)
    search = Driver.find_element_by_name('emailAddress')
    email = Email
    for character in email:
        search.send_keys(character)
        time.sleep(random.uniform(0.075,0.135))
    search = Driver.find_element_by_name('password')
    password = Password
    for character in password:
        search.send_keys(character)
        time.sleep(random.uniform(0.075,0.135))
    time.sleep(0.5)
    search = Driver.find_element_by_xpath("//input[@type='button']")
    search.click()



#Compte 1
login("Mail", "Password")

So it'll do everything well and open two browsers and do the things, but once he finishes handling all the functions he'll close all browsers, and I don't want that... When I put the code outside of the function it'll work well but when in it'll just close when all the functions are finished. I don't want it to close the browser because I'll need to wait something, but I just want Selenium to do the program and let the browser open and keep doing the program on other browsers...

2
  • 2
    Please post some code that shows the exact "problem" and explain what you are trying to achieve. – Anonymous Apr 8 at 18:21
  • Also to clarify - why do you not want it to close the browser? Technical reason? May affect the solution – Mark Mayo Apr 8 at 22:51
3

You are creating the Browser object inside the login function. When it ends, it is eligible for garbage collection.

You can pass a pointer to an object outside the function, so it will still have a reference when the function ends.

driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe",options=chrome_options);  

login("Mail", "Password", driver)
4
  • Yeah I already tried it but the problem I encounter with this is that i need to create for exemple ` driver1 = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe",options=chrome_options); driver2 = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe",options=chrome_options); ... login("Mail", "Password", driver1) login("Mail", "Password", driver2) ... ` But with this it'll open me all the browers before doing the tasks.... I want to open them one by one – Damsec Apr 9 at 9:21
  • What I found that works but idk if it's good or something is ; x = false at the start and while not x: time.sleep(100), with this it won't close all browser – Damsec Apr 9 at 9:27
  • Good answer. And, this approach supports the Page Object Model. @Damsec I highly recommend looking up Page Object Model design pattern as there is rarely a reason to use two driver variables (driver1, driver2) – Lee Jensen Apr 9 at 14:07
  • @LeeJensen Wow I didn't know of this !!! Thx a lot it'll be extremly helpful for me !!!!! – Damsec Apr 9 at 17:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.