admin管理员组

文章数量:1391990

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);

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!

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);

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!

Share Improve this question edited Apr 18, 2015 at 17:16 Erki M. 5,0722 gold badges52 silver badges77 bronze badges asked Oct 29, 2013 at 9:22 BiffyBiffy 8712 gold badges10 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Solution is to use proxy. Webdriver integrates very well with browsermob proxy: http://bmp.lightbody/

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\\./.*", 410);
    // Or whitelist what you need
    server.whitelistRequests("https?://*.*.yoursite./.*. 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;
}

Probably the easiest way to acplish what you want in a cross-browser way is to use a proxy. This would allow you to intercept requests for resources, and block them. This would also have the advantage of using the same code for all browsers, rather than having to special-case each browser with settings unique to that browser.

本文标签: javaSelenium Webdrivers Load Page without any resourcesStack Overflow