Google chrome closes immediately after being launched with selenium

后端 未结 8 981
说谎
说谎 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 09:56

    Just simply add:

    while(True):
        pass
    

    To the end of your function. It will be like this:

    def launchBrowser():
       chrome_options = Options()
       chrome_options.binary_location="../Google Chrome"
       chrome_options.add_argument("disable-infobars");
       driver = webdriver.Chrome(chrome_options=chrome_options)
    
       driver.get("http://www.google.com/")
       while(True):
           pass
    launchBrowser()
    
    0 讨论(0)
  • 2021-01-19 09:56
    def createSession():
        **global driver**
        driver = webdriver.Chrome(chrome_driver_path)
        driver.maximize_window()
        driver.get("https://google.com")
        return driver
    
    0 讨论(0)
  • 2021-01-19 10:01

    My solution is to define the driver in the init function first, then it won't close up the browser even the actional

    0 讨论(0)
  • 2021-01-19 10:03

    The browser is automatically disposed once the variable of the driver is out of scope. So, to avoid quitting the browser, you need to set the instance of the driver on a global variable:

    Dim driver As New ChromeDriver
    
    Private Sub Use_Chrome()
    
    driver.Get "https://www.google.com"
    '  driver.Quit
    End Sub
    
    0 讨论(0)
  • 2021-01-19 10:08

    This is somewhat old, but the answers on here didn't solve the issue. A little googling got me here

    http://chromedriver.chromium.org/getting-started

    The test code here uses sleep to keep the browser open. I'm not sure if there are better options, so I will update this as I learn.

    import time
    from selenium import webdriver
    
        driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
        driver.get('http://www.google.com/xhtml');
        time.sleep(5) # Let the user actually see something!
        search_box = driver.find_element_by_name('q')
        search_box.send_keys('ChromeDriver')
        search_box.submit()
        time.sleep(5) # Let the user actually see something!
        driver.quit() 
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题