因为 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)
我从 stackoverflow 得到代码 Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python
但是当我添加
driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com/")
到最后,它仍然加载图像:/
最佳答案
不幸的是,选项 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)
其他的东西也可以通过添加以下行来关闭:
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) ## Silverlight
关于javascript - 在 Selenium Python 中禁用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214473/