简体   繁体   English

JavaScript:在 Selenium ChromeDriver 中禁用图像

[英]JavaScript: Disable images in Selenium ChromeDriver

Because Webdriver waits for the entire page to load before going on to the next line, I want to disable images will speed things up when the.network is slow.因为 Webdriver 在继续下一行之前等待整个页面加载,所以我想禁用图像将在网络缓慢时加快速度。

This is the example js file in Selenium Webdriver's website:这是 Selenium Webdriver 网站中的示例 js 文件:

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
driver.quit();

How can I disable image in my code?如何在我的代码中禁用图像?

I have search google for this question, I only get this solution in Python: Disable images in Selenium Python .我在谷歌上搜索了这个问题,我只在 Python: Disable images in Selenium Python中得到这个解决方案。

On a high level, I see some solutions: 从高层次看,我看到一些解决方案:

  • set profile.managed_default_content_settings.images to 2 (I can't find the corresponding chromedriver documentation, but you can google it). profile.managed_default_content_settings.images设置为2 (我找不到相应的chromedriver文档,但您可以将其谷歌搜索)。
  • Set up a proxy . 设置代理 Connect to your page via a proxy that returns empty data when asking for an image file. 通过代理连接到您的页面,该代理在请求图像文件时返回空数据。
  • load the browser with a browser plugin that does this for you . 使用为您完成此操作的浏览器插件加载浏览器 Something (a bit like ad-blocked works) might be available already. 某些东西(有点像被广告屏蔽的作品)可能已经可用。 (con: browser-specific solution) (con:浏览器特定的解决方案)

Here I give you code for not loading image. 在这里,我为您提供了不加载图像的代码。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option( "prefs", {'profile.managed_default_content_settings.images': 2})
driver = webdriver.Chrome("chromedriver.exe",chrome_options=chrome_options)

You can pass an options object to WebdriverJS' Builder that disables images: 您可以将options对象传递给WebdriverJS的Builder以禁用图像:

{
    prefs: {
        profile: {
            managed_default_content_settings: {
                images: 2
            }
        }
    }
}

The complete example is: 完整的示例是:

const chromeDesktop = {
    prefs: {
        profile: {
            managed_default_content_settings: {
                images: 2
            }
        }
    }
};
const { By, Builder, until } = require('selenium-webdriver');
const driver = new Builder().withCapabilities(chromeDesktop).build();

This definitely worked for me. 这绝对对我有用。

      import { Options } from 'selenium-webdriver/chrome';
     
      const options = new Options();
    
    
      options.setUserPreferences({
    
        'profile.managed_default_content_settings.images': 2, //disable loading 
      });

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

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