Google chrome closes immediately after being launched with selenium

后端 未结 8 1108
说谎
说谎 2021-01-19 09:37

I am on Mac OS X using selenium with python 3.6.3.

This code runs fine, opens google chrome and chrome stays open.:

chrome_options = Options()
chrome         


        
8条回答
  •  情深已故
    2021-01-19 10:11

    My guess is that the driver gets garbage collected, in C++ the objects inside a function (or class) get destroyed when out of context. Python doesn´t work quite the same way but its a garbage collected language. Objects will be collected once they are no longer referenced.

    To solve your problem you could pass the object reference as an argument, or return it.

        def launchBrowser():
           chrome_options = Options()
           chrome_options.binary_location="../Google Chrome"
           chrome_options.add_argument("start-maximized");
           driver = webdriver.Chrome(chrome_options=chrome_options)
    
           driver.get("http://www.google.com/")
           return driver
        driver = launchBrowser()
    

提交回复
热议问题