javascript 在 Selenium Python 中禁用图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25214473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable images in Selenium Python
提问by Hyman
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 得到了代码不希望图像加载和 CSS 在 Firefox 上呈现在使用 Python 的 Selenium WebDriver 测试中
But when I add
但是当我添加
driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com/")
to the end, it still loads images :/
最后,它仍然加载图像:/
回答by kyrenia
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
回答by alecxe
UPDATE: The answer might not work any longer since permissions.default.image
became a frozen settingand cannot be changed. Please try with quickjava
extension (link to the answer).
更新:由于permissions.default.image
成为冻结设置且无法更改,因此该答案可能不再有效。请尝试使用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:
这就是它的显示方式:
回答by Luiz Geron
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.
这似乎没问题,因为我在每个驱动程序启动时创建了配置文件,因此实际上没有什么可迁移的。
回答by Anil
I understand this is a pythonquestion, but it helped me with facebook/php-webdriver. (First result in search engine for php webdriver disable javascript
)
我知道这是一个python问题,但它帮助我解决了facebook/php-webdriver。(搜索引擎中的第一个结果php webdriver disable javascript
)
I thought I'd post my code (altered version of @kyrenia answer for php) to help others get up and running.
我想我会发布我的代码(@kyrenia php答案的修改版本)来帮助其他人启动和运行。
Install Everything
安装一切
Download and install facebook/php-webdriver.
composer require facebook/webdriver
Download Selenium& Start it.
java -jar selenium-server-standalone-#.jar
Download Quick Javaand place it into your project directory.
下载并安装facebook/php-webdriver。
composer require facebook/webdriver
下载 Selenium并启动它。
java -jar selenium-server-standalone-#.jar
下载 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