不想让图像加载和CSS渲染在Firefox中使用Selenium WebDriver - Python
我正在用Python的Selenium 2来从我们合作伙伴的网站上获取一些数据。不过,平均下来,这个操作大约需要13秒。
我在寻找一种方法来禁用图片、CSS和Flash等内容。
我使用的是Firefox 3.6,并且还用pyvirtualdisplay来防止打开Firefox窗口。如果有其他加速Firefox的优化方法也很有帮助。
我已经尝试过network.http.*
的选项,但效果不大。
我还设置了permissions.default.image = 2
。
6 个回答
很遗憾,选项 firefox_profile.set_preference('permissions.default.image', 2)
在最新版本的Firefox中似乎不再有效,无法禁用图片。具体原因可以参考Alecxe对我问题的回答,链接在这里:无法在Selenium / Firefox中关闭图片。
我找到的最佳解决方案是使用Firefox扩展程序QuickJava,它可以禁用图片等功能。你可以在这里找到它:https://addons.mozilla.org/en-us/firefox/addon/quickjava/
我的Python代码如下:
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.add_extension(folder_xpi_file_saved_in + "\\quickjava-2.0.6-fx.xpi")
firefox_profile.set_preference("thatoneguydotnet.QuickJava.curVersion", "2.0.6.1") ## Prevents loading the 'thank you for installing screen'
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Images", 2) ## Turns images off
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.AnimatedImage", 2) ## Turns animated images off
driver = webdriver.Firefox(firefox_profile)
driver.get(web_address_desired)
禁用CSS(我想禁用Flash也可以)在Firefox的设置中仍然有效。不过,除了这些设置外,你还可以通过添加以下代码来关闭其他功能:
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.CSS", 2) ## CSS
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Cookies", 2) ## Cookies
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Flash", 2) ## Flash
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Java", 2) ## Java
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.JavaScript", 2) ## JavaScript
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2)
新编辑
我写这篇文章已经很久了,发现网页自动化(无论是测试还是爬虫/抓取)这个领域变化很大。主要的浏览器现在都提供了一个 --headless
的选项,甚至还有交互式的命令行界面。再也不用在Linux上修改老旧的 DISPLAY
变量了。
Firefox也发生了变化,迁移到了用Rust编写的 Servo 引擎。我尝试了下面的配置,使用的是一个较新的版本(具体来说是62.0)。有些有效,有些无效,大家要记住这一点。
我只是扩展了 kyrenia在这个问题中的回答。不过,禁用CSS可能会导致Jquery无法操作DOM元素。可以使用QuickJava和下面的内容:
profile.set_preference("network.http.pipelining", True)
profile.set_preference("network.http.proxy.pipelining", True)
profile.set_preference("network.http.pipelining.maxrequests", 8)
profile.set_preference("content.notify.interval", 500000)
profile.set_preference("content.notify.ontimer", True)
profile.set_preference("content.switch.threshold", 250000)
profile.set_preference("browser.cache.memory.capacity", 65536) # Increase the cache capacity.
profile.set_preference("browser.startup.homepage", "about:blank")
profile.set_preference("reader.parse-on-load.enabled", False) # Disable reader, we won't need that.
profile.set_preference("browser.pocket.enabled", False) # Duck pocket too!
profile.set_preference("loop.enabled", False)
profile.set_preference("browser.chrome.toolbar_style", 1) # Text on Toolbar instead of icons
profile.set_preference("browser.display.show_image_placeholders", False) # Don't show thumbnails on not loaded images.
profile.set_preference("browser.display.use_document_colors", False) # Don't show document colors.
profile.set_preference("browser.display.use_document_fonts", 0) # Don't load document fonts.
profile.set_preference("browser.display.use_system_colors", True) # Use system colors.
profile.set_preference("browser.formfill.enable", False) # Autofill on forms disabled.
profile.set_preference("browser.helperApps.deleteTempFileOnExit", True) # Delete temprorary files.
profile.set_preference("browser.shell.checkDefaultBrowser", False)
profile.set_preference("browser.startup.homepage", "about:blank")
profile.set_preference("browser.startup.page", 0) # blank
profile.set_preference("browser.tabs.forceHide", True) # Disable tabs, We won't need that.
profile.set_preference("browser.urlbar.autoFill", False) # Disable autofill on URL bar.
profile.set_preference("browser.urlbar.autocomplete.enabled", False) # Disable autocomplete on URL bar.
profile.set_preference("browser.urlbar.showPopup", False) # Disable list of URLs when typing on URL bar.
profile.set_preference("browser.urlbar.showSearch", False) # Disable search bar.
profile.set_preference("extensions.checkCompatibility", False) # Addon update disabled
profile.set_preference("extensions.checkUpdateSecurity", False)
profile.set_preference("extensions.update.autoUpdateEnabled", False)
profile.set_preference("extensions.update.enabled", False)
profile.set_preference("general.startup.browser", False)
profile.set_preference("plugin.default_plugin_disabled", False)
profile.set_preference("permissions.default.image", 2) # Image load disabled again
这段代码是干什么的呢?你可以在注释行中看到它的作用。不过,我还发现了一些关于:config的设置,可以提高性能。例如,上面的代码不加载文档的字体或颜色,但它加载了CSS,这样Jquery或其他库就可以操作DOM元素而不会报错。(如果需要进一步调试,你仍然可以下载CSS,但浏览器会跳过那些包含特殊字体或颜色定义的行。所以浏览器会下载和加载CSS,但会使用系统默认的样式,从而更快地渲染页面。)
想了解更多信息,可以 查看这篇文章。
编辑(测试)
我刚做了一个性能测试。你不必太认真对待这个结果,因为我只做了一次测试,主要是让你有个概念。
我在一台老机器上进行测试,配置是2.2GHz的Intel Pentium处理器,3GB内存和4GB的交换区,系统是Ubuntu 14.04 x64。
测试分为三个步骤:
- 驱动加载性能:在
webdriver
模块中加载驱动所浪费的时间。 - 页面加载性能:加载页面所浪费的时间。这也包括网络速度,不过渲染过程也算在内。
- DOM检查性能:在页面上检查DOM的速度。
我使用 这个页面 作为测试对象,检查 .xxy a
作为CSS选择器。然后我逐个使用了特殊的处理。
Selenium, Firefox, 无配置文件
Driver Loading Performance: 13.124099016189575
Page Loading Performance: 3.2673521041870117
DOM Inspecting Performance: 67.82778096199036
Selenium, Firefox, 上面的配置文件
Driver Loading Performance: 7.535895824432373
Page Loading Performance: 2.9704301357269287
DOM Inspecting Performance: 64.25136017799377
编辑(关于无头模式)
我大约一个月前做过一个测试,但没有记录结果。不过,我想提一下,当使用无头模式的Firefox时,驱动加载、页面加载和DOM检查的速度都能降到 十秒以下,这真的很酷。
我找到了一种方法,可以让Firefox浏览器不加载CSS、图片和Flash内容。
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
def disableImages(self):
## get the Firefox profile object
firefoxProfile = FirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
'false')
## Set the modified profile while creating the browser object
self.browserHandle = webdriver.Firefox(firefoxProfile)
再次感谢@Simon和@ernie给我的建议。