python 解決 webdriver Firefox 內存佔用問題

現象:運行selenium 做網頁自動化時,剛開始速度正常,但運行一段時間後速度明顯變慢,查看cpu佔用情況,發現慢的原因是firefox的cpu佔用達100%。估計是緩存問題。解決辦法:

一、定時重啓頁面:

webdriver.refresh()                    測試有效

也有介紹調用:webdriver.delete_all_cookies()            此方法測試無效

二、通過修改fireprofile優化內存及cpu佔用(有效):

profile = webdriver.FirefoxProfile()

profile.set_preference("permissions.default.image", 2)  #禁止下載圖片,根據情況使用

# 禁用瀏覽器緩存

profile.set_preference("network.http.use-cache", False)

profile.set_preference("browser.cache.memory.enable", False)

profile.set_preference("browser.cache.disk.enable", False)

profile.set_preference("browser.sessionhistory.max_total_viewers", 3)

profile.set_preference("network.dns.disableIPv6", True)

profile.set_preference("Content.notify.interval", 750000)

profile.set_preference("content.notify.backoffcount", 3)

# 有的網站支持 有的不支持 2 35 profile.set_preference("network.http.pipelining", True)

profile.set_preference("network.http.proxy.pipelining", True)

profile.set_preference("network.http.pipelining.maxrequests", 32)

三、最有效的辦法是第一第二步同步進行,運行一段時間重啓頁面。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章