Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進程的解決辦法(java版+python版)
selenium操作chrome瀏覽器需要有ChromeDriver驅(qū)動來協(xié)助。webdriver中關(guān)瀏覽器關(guān)閉有兩個方法,一個叫quit,一個叫close。
/** * Close the current window, quitting the browser if it's the last window currently open. */ void close(); /** * Quits this driver, closing every associated window. */ void quit();
通過查看以上官方聲明文檔,可以看出close方法是關(guān)閉當(dāng)前窗口,這個當(dāng)前如何理解?就是driver實例操作的頁面,叫當(dāng)前。如果當(dāng)前窗口只有一個tab,那么這個close方法就相當(dāng)于關(guān)閉了瀏覽器。quit方法就是直接退出并關(guān)閉所有關(guān)聯(lián)的tab窗口。所以,close方法一般關(guān)閉一個tab,quit方法才是我們認為的完全關(guān)閉瀏覽器方法。為了證明這個,我們用一個例子去演示:
package lessons; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FindElement_LinkText { public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.baidu.com"); driver.close(); //driver.quit(); } }
通過切換注銷最后兩行代碼,分別運行,觀察這兩種方法的實際效果。當(dāng)使用close方法的時候,因為只有百度首頁這個tab,所以會關(guān)閉瀏覽器,但是通過查看任務(wù)管理器發(fā)現(xiàn),ChromeDriver進程仍存在內(nèi)存中。如果使用quit方法,整個瀏覽器都直接關(guān)閉,ChromeDriver進程也會被結(jié)束。
ChromeDriver是輕量級的服務(wù),在單任務(wù)或不需要頻繁啟動瀏覽器的情況下,使用driver.quit()關(guān)閉瀏覽器,可以正常結(jié)束ChromeDriver進程。若在一個比較大的 測試套件中頻繁的啟動關(guān)閉,會增加一個比較明顯的延時導(dǎo)致瀏覽器進程不被關(guān)閉的情況發(fā)生,為了避免這一狀況我們可以通過ChromeDriverService來控制ChromeDriver進程的生死,達到用完就關(guān)閉的效果避免進程占用情況出現(xiàn)(Running the server in a child process)。具體實現(xiàn)如下:
ChromeDriverService service = new ChromeDriverService.Builder() .usingChromeDriverExecutable(new File("E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe")).usingAnyFreePort().build(); service.start(); driver = new ChromeDriver(); driver.get("http://www.baidu.com"); driver.quit(); // 關(guān)閉 ChromeDriver 接口 service.stop();
以上討論的均是java版的實現(xiàn),對于python來說是使用service庫來實現(xiàn)控制chromedriver的開啟和關(guān)閉。
from selenium.webdriver.chrome.service import Service
創(chuàng)建的時候需要把chromedriver.exe的位置寫在Service的XXX部分,需要調(diào)用他的命令行方法,不然報錯,然后啟動就可以了。
c_service = Service('xxx') c_service.command_line_args() c_service.start() driver = webdriver.Chrome() driver.get("http://www.baidu.com")
關(guān)閉的時候用quit而不是采用close,close只會關(guān)閉當(dāng)前頁面,quit會退出驅(qū)動并且關(guān)閉所關(guān)聯(lián)的所有窗口,最后執(zhí)行完以后就關(guān)閉。
driver.quit() c_service.stop()
嫌麻煩也可以直接使用python的os模塊執(zhí)行下面兩句話結(jié)束進程
os.system('taskkill /im chromedriver.exe /F') os.system('taskkill /im chrome.exe /F')
到此這篇關(guān)于Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進程的解決辦法(java版+python版)的文章就介紹到這了,更多相關(guān)Selenium關(guān)閉chromedriver/geckodriver進程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python第三方庫undetected_chromedriver的使用
- python一招完美搞定Chromedriver的自動更新問題
- Python3爬蟲ChromeDriver的安裝實例
- 詳解Python+Selenium+ChromeDriver的配置和問題解決
- python+selenium+chromedriver實現(xiàn)爬蟲示例代碼
- 下載與當(dāng)前Chrome對應(yīng)的chromedriver.exe(用于python+selenium)
- python selenium 執(zhí)行完畢關(guān)閉chromedriver進程示例
- python3?chromedrivers簽到的簡單實現(xiàn)
相關(guān)文章
使用Python中的reduce()函數(shù)求積的實例
今天小編就為大家分享一篇使用Python中的reduce()函數(shù)求積的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python opencv實現(xiàn)目標區(qū)域裁剪功能
這篇文章主要介紹了python opencv實現(xiàn)目標區(qū)域裁剪功能,通過截取到坐標信息以后用CV2的裁剪就可以完美實現(xiàn),本文給大家分享實例代碼,需要的朋友可以參考下2021-07-07Python基于Gensim實現(xiàn)文本相似度/匹配/查重
Gensim是基于Python語言的自然語言處理庫,用來主題建模、文本相似度等文本處理任務(wù),下面我們就來看看如何使用Gensim實現(xiàn)文本相似度/匹配/查重等操作吧2024-03-03Pandas DataFrame實現(xiàn)任意位置插入一列或一行
Pandas是Python中最流行的數(shù)據(jù)處理和分析庫之一,在數(shù)據(jù)分析過程中,有時候需要在Dataframe中插入新的數(shù)據(jù)列,本文主要介紹了Pandas DataFrame實現(xiàn)任意位置插入一列或一行,具有一定的參考價值,感興趣的可以了解一下2023-08-08