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

ws51t4hk  于 2023-11-14  发布在  Go
关注(0)|答案(1)|浏览(139)

你好,我正试图在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浏览器立即关闭。
我甚至删除了代码驱动程序。quit()来看看这是否有什么不同,但仍然是同样的问题。
谢谢

vcirk6k6

vcirk6k61#

修改代码如下:

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)

字符串
有关detach选项的更多详细信息,请参阅此答案:https://stackoverflow.com/a/77402087/7598774

相关问题