现象:运行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)
三、最有效的办法是第一第二步同步进行,运行一段时间重启页面。