我正在制作一个机器人程序,通过chromedriver从网上删除一些信息。但由于这些信息有时仅限于一个帐户(如instagram insights(,我需要使用几个UserData文件夹来保存chromedriver的所有登录等。为了节省一些内存,我制作了一个函数";init";其用期望的UserData Folder初始化chromedriver窗口。功能:
def init(userdata):
global driver
warnings.filterwarnings("ignore", category=DeprecationWarning)
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("window-size=fullscreen")
option.add_argument("--disable-extensions")
option.add_argument("--log-level=3")
option.add_argument("--headless")
option.add_argument(r'--user-data-dir=dirtoUserData' + str(userdata))
option.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
driver = webdriver.Chrome(chrome_options=option, executable_path='C:Windowschromedriver.exe')
当无头运行代码时,它就像一个魅力。然而,当使用无头时,会出现以下错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: was killed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:Program FilesGoogleChromeApplicationchrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
chrome_options
已经被弃用很长一段时间了。相反,您必须使用options
,如下所示:
driver = webdriver.Chrome(options=option, executable_path='C:Windowschromedriver.exe')