繁体   English   中英

运行 selenium chrome webdriver 后 Chrome 浏览器关闭

[英]Chrome browser closes after running selenium chrome webdriver

我目前正在学习 Selenium 4.0 并设置了一个基本脚本,它将单击 Python 网站上的一个按钮。 我正在使用 Chrome 网络驱动程序。 但是每当我运行我的代码时,chrome window 打开 Python 网站,然后立即关闭。 我如何保持打开状态?

浏览器版本和 webdriver 版本相同,我什至尝试过 Edge webdriver 并重新安装 Chrome。 我什至尝试将 webdriver 下载到我的本地目录,但这也不起作用。 这是我当前的脚本:

from selenium import webdriver

from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

driver.get("https://www.python.org/")
print(driver.title)
submit = driver.find_element(By.ID, "submit")
submit.click()

运行后,我的终端显示以下消息:

====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/user1/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver] found in cache
Welcome to Python.org

Process finished with exit code 0

好吧,这是正确的行为,因为它会正确执行您告诉它的所有操作。 事实上,您没有收到任何错误。 执行代码后,Chrome 驱动程序被杀死,因为 Python 应用程序已完成执行

如果您希望驱动程序打开的浏览器保持打开状态,请使用 Chrome 选项并添加分离

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM