Anu*_*ini 32 css python firefox selenium-webdriver
我正在使用Selenium 2和python绑定从我们的合作伙伴网站获取一些数据.但平均而言,我需要大约13秒才能执行此操作.
我正在寻找一种方法来禁用图像css和flash等.
我正在使用Firefox 3.6并使用pyvirtualdisplay来防止打开firefox窗口.任何其他优化加速Firefox也将有所帮助.
我已经尝试过network.http.*选项,但没有多大帮助.
并且还设置了 permissions.default.image = 2
Anu*_*ini 56
我找到了一种方法来防止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)
Run Code Online (Sandbox Code Playgroud)
再次感谢@Simon和@ernie的建议.
kyr*_*nia 10
不幸的是,该选项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)
Run Code Online (Sandbox Code Playgroud)
禁用CSS(我认为闪存)仍然可以使用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)
Run Code Online (Sandbox Code Playgroud)
自从我写完这篇文章已经很久了,我可以说网络自动化领域(用于测试或爬行/抓取目的)已经发生了很大变化.主流浏览器已经提供了一个--headless标志甚至是交互式shell.不再改变DISPLAYLinux上的旧变量.
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
Run Code Online (Sandbox Code Playgroud)
它有什么作用?您实际上可以在注释行中看到它的作用.但是,我还发现了一些about:config条目来提高性能.例如,上面的代码不加载文档的字体或颜色,但它加载CSS,因此Jquery - 或任何其他库 - 可以操纵DOM元素并且不会引发错误.(对于进一步的调试,你还是下载CSS,但您的浏览器将跳转其中含有一种特殊的字体家庭或颜色定义的线条,所以浏览器会下载和加载CSS,但使用系统默认的造型和渲染网页速度更快. )
有关更多信息,请查看此文章.
我刚做了一次性能测试.你真的不需要把结果认真对待,因为我只做了一次这个测试,你就有了一个想法.
我在2.2 gHZ Intel Pentium处理器的旧机器上进行了测试,3 gB RAM,4gB交换区域,Ubuntu 14.04 x64系统.
测试需要三个步骤:
webdriver模块中加载驱动程序所浪费的秒数.我将此页面用作主题并.xxy a作为CSS选择器进行检查.然后我逐个使用了一个特殊的过程.
Driver Loading Performance: 13.124099016189575
Page Loading Performance: 3.2673521041870117
DOM Inspecting Performance: 67.82778096199036
Run Code Online (Sandbox Code Playgroud)
Driver Loading Performance: 7.535895824432373
Page Loading Performance: 2.9704301357269287
DOM Inspecting Performance: 64.25136017799377
Run Code Online (Sandbox Code Playgroud)
我可能在一个月前做过测试,但我无法取得结果.但是,我想提一下,当Firefox无头使用时,驱动程序加载,页面加载和DOM检查速度会在十秒内降低.那太酷了.
| 归档时间: |
|
| 查看次数: |
18100 次 |
| 最近记录: |