因为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)
我从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/")
到最后,它仍然加载图片:/
答案 0 :(得分:18)
不幸的是,选项firefox_profile.set_preference('permissions.default.image', 2)
将不再用于使用最新版本的Firefox禁用图像 - [原因请参阅Alecxe对我的问题的回答Can't turn off images in Selenium / Firefox]
我最好的解决方案是使用firefox扩展程序quickjava,其中包括禁用图像 - https://addons.mozilla.org/en-us/firefox/addon/quickjava/
我的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)
也可以通过添加以下内容来关闭其他内容:
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
答案 1 :(得分:17)
更新:答案可能不再适用permissions.default.image
became a frozen setting,因此无法更改。请尝试使用quickjava
扩展程序(链接到answer)。
您需要将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()
这就是它的显示方式:
答案 2 :(得分:4)
接受的答案对我来说也不起作用。从"原因" kyrenia提到的链接我收集到的Firefox会覆盖" permissions.default.image"首次启动时的偏好,我可以通过执行以下操作来阻止:
# Arbitrarily high number
profile.set_preference('browser.migration.version', 9001)
自从我在每个驱动程序启动时创建配置文件以来,似乎没有问题,因此实际上没有任何内容可以迁移。
答案 3 :(得分:2)
我理解这是一个python问题,但它帮助了我facebook/php-webdriver。 (php webdriver disable javascript
)搜索引擎的第一个结果
我以为我发布了我的代码(php的@kyrenia答案的更改版本)以帮助其他人启动并运行。
下载并安装facebook/php-webdriver。 composer require facebook/webdriver
Download Selenium&启动它。 java -jar selenium-server-standalone-#.jar
Download Quick Java并将其放入项目目录。
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
在此处查看更多偏好设置: https://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js