禁用Selenium Python中的图像 [英] Disable images in Selenium Python

查看:2841
本文介绍了禁用Selenium Python中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为Webdriver在进入下一行之前等待整个页面加载,我认为禁用图片,css和javascript会加快速度。

Because Webdriver waits for the entire page to load before going on to the next line, I think disabling images, css and javascript will speed things up.

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得到代码不希望加载图像和在Firefox上使用Python在Selenium WebDriver测试中使用Python渲染CSS

I got the code from 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/")

到结尾,它仍然加载图片:/

to the end, it still loads images :/

推荐答案

UPDATE :答案可能不再有效,因为 permissions.default.image 已成为冻结设置,无法更改。请尝试使用 quickjava 扩展程序(链接到 answer )。

UPDATE: The answer might not work any longer since permissions.default.image became a frozen setting and cannot be changed. Please try with quickjava extension (link to the answer).

您需要将 firefox_profile 实例传递给 webdriver 构造函数:

You need to pass firefox_profile instance to the webdriver constructor:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')

driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get('http://www.stackoverflow.com/')

driver.close()

是如何显示的:

这篇关于禁用Selenium Python中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆