[英]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: 从高层次看,我看到一些解决方案:
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文档,但您可以将其谷歌搜索)。 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.