不希望在Selenium WebDriver-Python中加载图像并在Firefox上呈现CSS

2024-05-16 00:26:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Selenium 2和python绑定从合作伙伴的站点获取一些数据。但我平均要花13秒来做这个手术。

我正在寻找一种方法来禁用图像css和flash等

我使用的是Firefox3.6,也使用了pyvirtualdisplay来防止打开Firefox窗口。任何其他加速firefox的优化也会有帮助。
我已经尝试过network.http.*选项,但没有多大帮助。

并设置permissions.default.image = 2


Tags: 数据方法图像http站点选项selenium合作伙伴
3条回答

我已经找到了一种方法来阻止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的建议。

不幸的是,选项firefox_profile.set_preference('permissions.default.image', 2)似乎不再能够使用最新版本的Firefox禁用图像-[原因请参阅Alecxe对我的问题的回答Can't turn off images in 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) 

新编辑

我写这篇文章已经很久了,我可以说web自动化领域(无论是测试还是爬行/抓取)已经发生了很大的变化。主要浏览器已经提供了一个--headless标志,甚至是交互式shell。不再更改Linux上的好的旧DISPLAY变量。

Firefox也发生了变化,迁移到了用Rust编写的Servo引擎。我试过下面的一个当代版本的简介(特别是62.0)。有的工作了,有的没有。记住这一点。


我只是在这个问题上扩展the answer of 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

它是做什么的?实际上,您可以在注释行中看到它的作用。不过,我也找到了一些about:config条目来提高性能。例如,上面的代码不加载文档的字体或颜色,但它加载CSS,因此Jquery-或任何其他库-可以操作DOM元素并且不会引发错误。(为了进一步调试,您仍然可以下载CSS,但是您的浏览器将跳转包含特殊字体系列或颜色定义的行。因此,浏览器将下载并加载CSS,但在样式设置中使用系统默认值并更快地呈现页面。)

有关详细信息,check out this article


编辑(测试)

我刚做了一个性能测试。你不必把结果当回事,因为我只做了一次测试,让你有个想法。

我在一台旧机器上进行了测试,使用2.2ghz英特尔奔腾处理器,3gb内存,4gB交换区,Ubuntu 14.04x64系统。

测试分为三个步骤:

  • 驱动程序加载性能:在webdriver模块中加载驱动程序所浪费的秒数。
  • 页面加载性能:加载页面所浪费的秒数。它还包括互联网速度,但渲染过程也包括在内。
  • DOM检查性能:页面上的DOM检查速度。

我使用this page作为主题,检查.xxy a作为CSS选择器。然后我一个接一个地使用了一个特殊的过程。

硒,Firefox,无配置文件

Driver Loading Performance: 13.124099016189575
Page Loading Performance: 3.2673521041870117
DOM Inspecting Performance: 67.82778096199036

硒,火狐,上图

Driver Loading Performance: 7.535895824432373
Page Loading Performance: 2.9704301357269287
DOM Inspecting Performance: 64.25136017799377

编辑(关于无头)

一个月前我做了一个测试,但是我不能接受结果。不过,我想提一下,当Firefox使用headless时,驱动程序加载、页面加载和DOM检查速度在10秒以下降低。真的很酷。

相关问题 更多 >