我正在使用Selenium 2和python绑定从我们的合作伙伴网站获取一些数据.但平均而言,我需要大约13秒才能执行此操作.
我正在寻找一种方法来禁用图像css和flash等.
我正在使用Firefox 3.6并使用pyvirtualdisplay来防止打开firefox窗口.任何其他优化加速Firefox也将有所帮助.
我已经尝试过network.http.*选项,但没有多大帮助.
并且还设置了 permissions.default.image = 2
因为Webdriver在进入下一行之前等待整个页面加载,我认为禁用图像,css和javascript会加快速度.
from selenium import webdriver
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)
我从stackoverflow获取代码不希望加载图像和使用Python在Selenium WebDriver测试中在Firefox上渲染CSS
但是当我补充说
driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com/")
Run Code Online (Sandbox Code Playgroud)
到最后,它仍然加载图像:/
我读过类似的问题,其中一个应该是答案,但是当我尝试它时,它只给出了部分解决方案.
我指的是问题:在Selenium Python中禁用图像
我的问题是我尝试了这个解决方案并且一些图像没有出现,但图像来自:
<img href="www.xxx.png">
Run Code Online (Sandbox Code Playgroud)
正在加载.有没有办法告诉firefox/selenium不要得到它?如果没有,有没有办法从我回来的dom元素中丢弃它,通过
self._browser.get(url)
content=self._browser.page_source
Run Code Online (Sandbox Code Playgroud)
例如,通过在dom树上做某种查找替换?
浏览器配置与上一个问题的浏览器相同:
firefox_profile = webdriver.FirefoxProfile()
# Disable CSS
firefox_profile.set_preference('permissions.default.stylesheet', 2)
# Disable images
firefox_profile.set_preference('permissions.default.image', 2)
# Disable Flash
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
# Set the modified profile while creating the browser object
self._browser = webdriver.Firefox(firefox_profile=firefox_profile)
Run Code Online (Sandbox Code Playgroud)
- - - - - - - - - - - - - - - - - - -更正 - - - - - - -------------------------
我继续挖掘,我学到的是那个,
如果我检查文本文件,selenium/firefox组合做了我看到它,它没有带来图像,然后保持链接.
但当我这样做时:
self._browser.save_screenshot("info.png")
Run Code Online (Sandbox Code Playgroud)
我有一个24兆的文件加载了所有的img链接.
有人能解释一下这件事吗?
谢谢