如何关闭 Python selenium webdriver 窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42591175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:55:35  来源:igfitidea点击:

how to close Python selenium webdriver window

pythonseleniumwebdriver

提问by Kok chuan Lim

I have a python script that scraps data off from a website on an hourly basis. It is stored on the server at the moment and is working well as I am using task scheduler to schedule it to execute the script on an hourly basis.

我有一个 python 脚本,它每小时从网站上抓取数据。它目前存储在服务器上并且运行良好,因为我正在使用任务调度程序来安排它每小时执行一次脚本。

I am using this code

我正在使用此代码

driver.quit()

to quit the browser window

退出浏览器窗口

My problem to this that whenever I am not logging in to the server, it will start stacking up the webdriver window as somehow the driver.quit() function does not work when I am logging into the server. every morning when I came to work, I have tons of window to close from the server.

我的问题是,每当我没有登录到服务器时,它就会开始堆叠 webdriver 窗口,因为当我登录到服务器时 driver.quit() 函数以某种方式不起作用。每天早上上班时,我都有很多窗口要从服务器关闭。

I tried to quit, close, dispose, but it doesn't help. What else I can try?

我试图退出,关闭,处理,但没有帮助。我还能尝试什么?

回答by jeanhuguesroy

In python using chromedriver, I quit Chrome processes with:

在使用 chromedriver 的 python 中,我退出了 Chrome 进程:

driver.close()

回答by tommystarlit

For Python and chromedriver I've found these two methods useful (mind the difference):

对于 Python 和 chromedriver,我发现这两种方法很有用(注意区别):

driver.close()- closes the browser active window.

driver.close()- 关闭浏览器活动窗口。

driver.quit()- closes all browser windows and ends driver's session/process.

driver.quit()- 关闭所有浏览器窗口并结束驱动程序的会话/进程。

回答by Guangmin Shen

I use linux command to close all. Here're two commands I ran after all scraping job is done:

我使用 linux 命令关闭所有。这是我在所有抓取工作完成后运行的两个命令:

  • pkill firefox
  • pkill Xcfb
  • 杀火狐
  • 杀戮Xcfb

回答by Pike D.

I think webDriver.Dispose()should work, It closes all browser windows. Here is a SO post about the 3 different ways to close a webdriver.

我认为webDriver.Dispose()应该可以,它会关闭所有浏览器窗口。这是关于关闭 webdriver 的 3 种不同方法SO 帖子