How to disable Javascript when using Selenium?

前端 未结 16 727
星月不相逢
星月不相逢 2020-12-05 05:07

I am wondering how do I disable javascript when using selenium so I can test server side validation.

I found this article but I don\'t know what to really do. Like I

16条回答
  •  难免孤独
    2020-12-05 05:43

    I was trying to solve this problem and found this blog but the first post has a link that is no longer valid. But I finally found the code to place inside the user-extensions.js that works. Here it is:

    Selenium.prototype.doDisableJavascript = function() {
        setJavascriptPref(false);
    };
    
    Selenium.prototype.doEnableJavascript = function() {
        setJavascriptPref(true);
    };
    
    function setJavascriptPref(bool) {
       prefs = Components.classes["@mozilla.org/preferences-service;1"]
               .getService(Components.interfaces.nsIPrefBranch);
       prefs.setBoolPref("javascript.enabled", bool);
    }
    

    Hope this save the time it took me to find it.

提交回复
热议问题