简体   繁体   English

在Selenium Python中禁用图像

[英]Disable images in Selenium Python

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. 因为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)

I got the code from stackoverflow Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python 我从stackoverflow获取代码不希望加载图像和使用Python在Selenium WebDriver测试中在Firefox上渲染CSS

But when I add 但是当我补充说

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

to the end, it still loads images :/ 到最后,它仍然加载图像:/

Unfortunately the option firefox_profile.set_preference('permissions.default.image', 2) will no longer work to disable images with the latest version of Firefox - [for reason see Alecxe's answer to my question Can't turn off images in Selenium / Firefox ] 不幸的是,选项firefox_profile.set_preference('permissions.default.image', 2)将不再用于使用最新版本的Firefox禁用图像 - [原因见Alecxe对我的问题的回答无法关闭Selenium / Firefox中的图像 ]

The best solution i had was to use the firefox extension quickjava , which amongst other things can disable images- https://addons.mozilla.org/en-us/firefox/addon/quickjava/ 我最好的解决方案是使用firefox扩展quickjava,其中包括禁用图像 - https://addons.mozilla.org/en-us/firefox/addon/quickjava/

My Python code: 我的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)

Other things can also be switched off by adding the lines: 其他东西也可以通过添加行来关闭:

  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

UPDATE : The answer might not work any longer since permissions.default.image became a frozen setting and cannot be changed. 更新 :答案可能不再有效,因为permissions.default.image变为冻结设置且无法更改。 Please try with quickjava extension (link to the answer ). 请尝试使用quickjava扩展(链接到答案 )。


You need to pass firefox_profile instance to the webdriver constructor: 您需要将firefox_profile实例传递给webdriver构造函数:

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()

And this is how it would be displayed: 这就是它的显示方式:

在此输入图像描述

The accepted answer doesn't work for me either. 接受的答案对我也不起作用。 From the "reason" links referred by kyrenia I gathered that Firefox overrides the "permissions.default.image" preference on the first startup and I was able to prevent that by doing: 从kyrenia提到的“原因”链接我收集到Firefox在第一次启动时覆盖了“permissions.default.image”首选项,我可以通过执行以下操作来阻止:

# Arbitrarily high number
profile.set_preference('browser.migration.version', 9001)

Which seems to be ok since I create the profile on each driver startup so there is nothing to actually be migrated. 这似乎没问题,因为我在每个驱动程序启动时创建配置文件,因此实际上没有任何实际迁移。

I understand this is a question, but it helped me with facebook/php-webdriver . 我知道这是一个问题,但它帮助我用facebook / php-webdriver (First result in search engine for php webdriver disable javascript ) (第一个结果在搜索引擎的php webdriver disable javascript

I thought I'd post my code (altered version of @kyrenia answer for ) to help others get up and running. 我以为我会发布我的代码(改变版本的@kyrenia回答 )来帮助其他人起床和运行。


Install Everything 安装一切

  1. Download and install facebook/php-webdriver . 下载并安装facebook / php-webdriver composer require facebook/webdriver

  2. Download Selenium & Start it. 下载Selenium并启动它。 java -jar selenium-server-standalone-#.jar

  3. Download Quick Java and place it into your project directory. 下载Quick Java并将其放入项目目录中。


Usage 用法

use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

// Change this to the path of you xpi
$extensionPath = $this->container->getParameter('kernel.root_dir').'/../bin/selenium/quickjava-2.0.6-fx.xpi';

// Build our firefox profile
$profile = new FirefoxProfile();
$profile->addExtension($extensionPath);
$profile->setPreference('thatoneguydotnet.QuickJava.curVersion', '2.0.6.1');
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Images', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.CSS', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Cookies', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Flash', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Java', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.JavaScript', 2);
$profile->setPreference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2);

// Create DC
$dc = DesiredCapabilities::firefox();
$dc->setCapability(FirefoxDriver::PROFILE, $profile);

// Create our new driver
$driver = RemoteWebDriver::create($host, $dc);
$driver->get('http://stackoverflow.com');

// The HTML Source code
$html = $driver->getPageSource();

// Firefox should be open and you can see no images or css was loaded

View more preference settings here: https://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js 在此处查看更多偏好设置: https//github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM