Selenium 打开后立即关闭。

3

我的Python版本是3.9.5。每次使用driver.get函数时,Chrome浏览器会出现一秒钟,然后立即关闭窗口。我该如何让它保持打开状态?以前它能正常工作,但当我关闭VSCode并重新打开它时,我做了一些导致Chrome似乎没有被打开的操作。


你在控制台/终端中运行代码以查看错误消息了吗?如果您想停止脚本在结尾处关闭,则必须在结尾处停止退出脚本 - 即使用 input(),它将等待在控制台中按任意键退出脚本。 - furas
2个回答

2

我认为你可以通过添加detach选项来解决这个问题。
例如:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

0

请检查日志以获取更多信息。有时您的网络浏览器已更新,但chromedriver与其版本不匹配,因此您需要下载特定版本的浏览器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接