Selenium Webdrivers:无需任何资源即可加载页面 [英] Selenium Webdrivers: Load Page without any resources
问题描述
我正在尝试阻止Javascript更改我正在使用Selenium测试的网站源代码。问题是,我不能简单地在Webdriver中关闭Javascript,因为我需要它来进行测试。这是我正在为Firefox Webdriver做的事情:
I am trying to prevent Javascript from changing the site's source code I'm testing with Selenium. The problem is, I can't just simply turn Javascript off in the Webdriver, because I need it for a test. Here's what I'm doing for the Firefox Webdriver:
firefoxProfile.setPreference("permissions.default.image", 2);
firefoxProfile.setPreference("permissions.default.script", 2);
firefoxProfile.setPreference("permissions.default.stylesheet", 2);
firefoxProfile.setPreference("permissions.default.subdocument", 2);
我不允许Firefox加载任何图像,脚本和样式表。
如何使用Internet Explorer Webdriver和Chrome Webdriver执行此操作?我没有找到任何类似的偏好。或者甚至有更优雅的方法来阻止webdrivers加载网站的JS文件?
谢谢!
I don't allow Firefox to load any Images, Scripts and Stylesheets. How can I do this with the Internet Explorer Webdriver and the Chrome Webdriver? I have not found any similar preferences. Or is there even a more elegant way to stop the webdrivers from loading the site's JS Files after all? Thank you!
推荐答案
解决方法是使用代理。 Webdriver与browsermob代理集成得非常好: http://bmp.lightbody.net/
Solution is to use proxy. Webdriver integrates very well with browsermob proxy: http://bmp.lightbody.net/
private WebDriver initializeDriver() throws Exception {
// Start the server and get the selenium proxy object
ProxyServer server = new ProxyServer(proxy_port); // package net.lightbody.bmp.proxy
server.start();
server.setCaptureHeaders(true);
// Blacklist google analytics
server.blacklistRequests("https?://.*\\.google-analytics\\.com/.*", 410);
// Or whitelist what you need
server.whitelistRequests("https?://*.*.yoursite.com/.*. https://*.*.someOtherYourSite.*".split(","), 200);
Proxy proxy = server.seleniumProxy(); // Proxy is package org.openqa.selenium.Proxy
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// start the driver ;
Webdriver driver = new FirefoxDriver(capabilities);
//WebDriver driver = new InternetExplorerDriver();
return driver;
}
这篇关于Selenium Webdrivers:无需任何资源即可加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!