首页 > 解决方案 > 使用 Selenium Webdriver 启动后,Google chrome 会自动关闭

问题描述

我在 Windows 10 上使用 Selenium 和 Python 3.7.3。

如果我将代码包装在一个类中,浏览器会在打开页面后立即终止:

'''
Program to show how to open chrome browser using selenium webdriver
'''

from selenium import webdriver
#import os

class run_chrome_tests(object):

    def test_method(self):

        # This is the location of the chrome driver saved into a variable
        #driver_location = "D:\\Udemy_Python\\Libs\\chromedriver.exe"

        # Letting the system environment know the location of the chrome driver
        #os.environ["webdriver.chrome.driver"] = driver_location

        # Letting the chrome browser know the location of the chrome driver
        driver = webdriver.Chrome()

        driver.get("http://www.letskodeit.com")


ch = run_chrome_tests()
ch.test_method()

标签: seleniumselenium-webdriverselenium-chromedriver

解决方案


检查 Chrome 浏览器的版本和 chrome 驱动程序的版本,如果驱动程序不兼容,则浏览器在打开后立即终止,请尝试同时使用最新版本


推荐阅读