【问题标题】:Google chrome closes immediately after being launched with selenium谷歌浏览器在使用 selenium 启动后立即关闭
【发布时间】:2018-05-10 13:10:50
【问题描述】:

我在 Mac OS X 上使用 selenium 和 python 3.6.3。

这段代码运行良好,打开谷歌浏览器并且浏览器保持打开状态。:

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/")

但是由于代码封装在函数中,浏览器在打开页面后立即终止:

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/")
launchBrowser()

我想在保持浏览器打开的同时在函数中使用相同的代码。

【问题讨论】:

  • 不确定这是否是您的问题的原因。但是您的代码在这两个示例之间有所不同。 "disable-infobars""start-maximized"

标签: python google-chrome selenium selenium-chromedriver


【解决方案1】:

为防止这种情况发生,请确保您的 driver 变量在您的函数之外定义或定义为全局变量,以防止在函数完成执行后立即对其进行垃圾回收。

在上面的示例中,这可能意味着:

driver = webdriver.Chrome(chrome_options=get_options())

def get_options():
   chrome_options = Options()
   chrome_options.binary_location="../Google Chrome"
   chrome_options.add_argument("disable-infobars")
   return chrome_options

def launchBrowser():
   driver.get("http://www.google.com/")

launchBrowser()

【讨论】:

    【解决方案2】:

    谁有同样的问题,把驱动设置成全局就好,简单如下:

    global driver
    driver.get("http://www.google.com/")
    

    这解决了我这边的问题,希望这会有所帮助

    【讨论】:

      【解决方案3】:

      为了让浏览器保持打开状态,我这样做:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      def browser_function():
          driver_path = "path/to/chromedriver"
          chr_options = Options()
          chr_options.add_experimental_option("detach", True)
          chr_driver = webdriver.Chrome(driver_path, options=chr_options)
          chr_driver.get("https://target_website.com")
      

      【讨论】:

        【解决方案4】:

        只需添加:

        while(True):
            pass
        

        到你的功能结束。会是这样的:

        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()
        

        【讨论】:

          【解决方案5】:

          我的解决方案是先在init函数中定义驱动,然后它不会关闭浏览器,即使是actional

          【讨论】:

            【解决方案6】:

            一旦驱动程序的变量超出范围,浏览器就会自动释放。 因此,为避免退出浏览器,您需要在全局变量上设置驱动程序的实例:

            Dim driver As New ChromeDriver
            
            Private Sub Use_Chrome()
            
            driver.Get "https://www.google.com"
            '  driver.Quit
            End Sub
            

            【讨论】:

              【解决方案7】:
              def createSession():
                  **global driver**
                  driver = webdriver.Chrome(chrome_driver_path)
                  driver.maximize_window()
                  driver.get("https://google.com")
                  return driver
              

              【讨论】:

              • 请为您的答案添加解释。
              • 通过将驱动程序设置为 init 方法中的全局变量有效。
              【解决方案8】:

              这有点旧,但这里的答案并没有解决问题。一点谷歌搜索让我来到这里

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

              这里的测试代码使用睡眠来保持浏览器打开。我不确定是否有更好的选择,所以我会在学习时更新。

              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() 
              

              【讨论】:

                【解决方案9】:

                根据您分享的代码块,可能有以下 3 种可能的解决方案:

                • binary_location 中,您必须将 .. 更改为 .. 表示项目工作区)
                • binary_location 中,您必须将 .. 更改为 /myspace/chrome(绝对 Chrome 二进制)
                • 在启动驱动时,添加开关executable_path

                  driver = webdriver.Chrome(chrome_options=options, executable_path=r'/your_path/chromedriver')
                  

                【讨论】:

                  【解决方案10】:

                  我的猜测是驱动程序会被垃圾收集,在 C++ 中,函数(或类)内的对象在脱离上下文时会被销毁。 Python 的工作方式并不完全相同,但它是一种垃圾收集语言。不再引用的对象将被收集。

                  要解决您的问题,您可以将对象引用作为参数传递,或将其返回。

                      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()
                  

                  【讨论】:

                  • 这个答案应该被检查!为解释 垃圾 概念 +1 票。显然这是它的主要原因
                  • 嗨。如果我关闭 python 程序,这个概念仍然有效吗?就我而言,我正在使用 python 脚本打开一个网页。一旦页面被加载并且没有错误,我希望 python 程序结束。但我不想关闭浏览器。我想继续手动在浏览器上工作。我们能做到吗?
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-08-02
                  • 2020-03-13
                  • 1970-01-01
                  • 2020-03-23
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多