谷歌浏览器在使用 selenium 启动后立即关闭

问题描述 投票:0回答:16

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

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

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

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

python google-chrome selenium selenium-chromedriver
16个回答
24
投票

我的猜测是驱动程序被垃圾收集,在 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()

23
投票

只需添加:

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

10
投票

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

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

5
投票

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

Dim driver As New ChromeDriver

Private Sub Use_Chrome()

driver.Get "https://www.google.com"
'  driver.Quit
End Sub

3
投票

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

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

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

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

3
投票

在此处添加实验性选项分离真实作品:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)

2
投票

我的解决方法是先在init函数里定义驱动,然后连actional都不会关闭浏览器


2
投票

谁有同样的问题,只需将驱动程序设置为全局即可,如下所示:

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

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


0
投票

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

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

    executable_path

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

0
投票
def createSession():
    **global driver**
    driver = webdriver.Chrome(chrome_driver_path)
    driver.maximize_window()
    driver.get("https://google.com")
    return driver

0
投票

为防止这种情况发生,请确保您的

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

0
投票

嗨,以下解决方案对我有用请试试这些。

解决方案 1:Google chrome 使用 Selenium Web Driver 启动后自动关闭。

检查 Chrome 浏览器驱动程序 exe 文件的版本和您拥有的 google chrome 版本,如果驱动程序不兼容,则 selenium-web 驱动程序浏览器会在 selenium-web 驱动程序打开后终止,尝试根据您的 chrome 匹配驱动程序版本版本


0
投票

浏览器关闭的原因是程序结束,驱动变量在最后一行代码后被垃圾回收。对于帖子中的第二个代码,无论您是在函数中还是在全局范围内使用它都没有关系。解释完最后一条语句后,驱动程序变量被垃圾收集,浏览器从程序终止中终止。

解决方案: 设置 chrome 选项

使用时间模块

在程序末尾使用无限循环来延迟程序关闭


0
投票

您可以简单地添加:-

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)

0
投票

以上所有其他建议对我都不起作用。但是,我需要做的就是运行

pip uninstall selenium
,然后运行
pip install selenium
。我需要重新安装Selenium的原因是我在安装Chrome之前安装了Selenium,这导致Selenium Driver无法找到Chrome浏览器。因此,重新安装 Selenium 可确保其能够找到 Chrome 浏览器并自动将其注册为 Chrome 驱动程序。更深入的解释请看https://eokulik.com/now-you-dont-need-to-install-chromedriver/

在代码中,我确保指定“分离”选项。如果没有“分离”选项,Chrome 将在启动后自动关闭!

from selenium.webdriver.chrome.options import Options

r_options = Options()
r_options.add_experimental_option('detach', True)
driver = webdriver.Chrome(options=r_options)

0
投票

只需复制并粘贴即可克服各种错误和警告:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

chrome_driver_path = "path\to\your\chromedriver.exe"

options = Options()
options.add_argument("--remote-debugging-port=9222")  # Use an arbitrary port number
options.add_experimental_option("detach", True)  # Keep the browser window open after exiting the script

service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service, options=options)

driver.get("https://facebook.com")
© www.soinside.com 2019 - 2024. All rights reserved.