现象:运行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)

三、最有效的办法是第一第二步同步进行,运行一段时间重启页面。

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐