首页 > 解决方案 > 试图通过硒关闭便携式浏览器

问题描述

我正在尝试通过 selenium 关闭便携式浏览器

我通过了--remote-debugging-port=9222,因为如果我不通过它,那么程序就会陷入webdriver.Chrome(). 它将打开便携式浏览器,但不会加载 URL。

但是在打开 URL 后,我想关闭浏览器,但driver.quit()它对我不起作用。我已经尝试了一些其他方法来关闭浏览器,但它们也不起作用。

我想关闭由该程序打开的浏览器的特定实例,而不是其他打开的浏览器实例。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.binary_location = 'C:/Portable/GoogleChromePortable/GoogleChromePortable.exe'
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--profile-directory=Person 1")
driver = webdriver.Chrome(options=chrome_options,executable_path='C:/Portabl/chromedriver_win32/chromedriver.exe')
url = "https://www.google.com/"
driver.get(url)
driver.quit()

我在用:

selenium 3.141.0, windows 10, python 3.8.0, portable chrome version 93.0.4577.63 (32-bit)

标签: python-3.xselenium-webdriverbrowserpython-webbrowser

解决方案


你的这个声明

我通过了 --remote-debugging-port=9222 因为如果我不通过它,那么程序就会卡在 webdriver.Chrome() 的对象创建中

是不正确的。--remote-debugging-port=9222看起来像是部署应用程序的端口号,并且您已使用 chrome 选项将它们发送到browser object.

driver.quit()

这通常应该有效,当它不起作用时错误是什么?

另外,要关闭单个实例,您可以这样做

driver.close()

看看是否有帮助。


推荐阅读