在写爬虫时会经常因为加载js等一些问题,不得不使用selenium模拟浏览器登陆,有的时候会打开多个浏览器,就会导致电脑变卡,内存不够,直接导致程序终止。
这里我以前的文章中说过可以用特定端口打开浏览器,有兴趣的可以去看看,这次主要是说quit和close
如果打开了,能及时关闭,也不会影响到什么,因此,这就用到了这两个方法。首先看一下这两个函数的描述。
def quit(self):
"""
Closes the browser and shuts down the ChromeDriver executable
that is started when starting the ChromeDriver
"""
try:
RemoteWebDriver.quit(self)
except Exception:
# We don't care about the message because something probably has gone wrong
pass
finally:
self.service.stop()
这里的意思就是说quit函数会把你打开浏览器的所有执行的操作全部停掉并且退出浏览器
def close(self):
"""
Closes the current window.
:Usage:
driver.close()
"""
self.execute(Command.CLOSE)
这里的意思是说close只会把当前的端口关闭
总结
两个的区别就是close是关闭当前窗口,如果只有一个窗口就会关闭浏览器,但并没有清除一些配置。而quit则是退出浏览器,并且清除了所有的配置。
因此,根据需求选择quit和close