提问人:Mr_Shoryuken 提问时间:11/7/2023 更新时间:11/7/2023 访问量:41
Chrome浏览器在使用selenium启动后立即关闭
Chrome browser closes immediately after being launched with selenium
问:
您好,我正在尝试在 python 中运行以下代码。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
def verify_title():
# Navigate to the website
driver.get("https://google.com")
# Get the title of the page
title = driver.title
# Verify the title
expected_title = "Google"
if title == expected_title:
print("Title verification successful!")
else:
print(f"Title verification failed. Expected '{expected_title}, but got '{title}'.")
# Close the browser
driver.quit()
if __name__ == '__main__':
verify_title()
它确实成功运行,并输出“标题验证成功!”,但 chrome 浏览器会立即关闭。
我什至删除了代码 driver.quit() 看看这是否有任何区别,但问题仍然相同。
谢谢。
答:
1赞
Shawn
11/7/2023
#1
修改代码如下:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)
有关选项的更多详细信息,请参阅此答案:https://stackoverflow.com/a/77402087/7598774detach
评论