29

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 make this javascript file then what?

http://thom.org.uk/2006/03/12/disabling-javascript-from-selenium/

1
  • 2
    I wrote that article - unfortunately, that method only works inside the Selenium IDE extension in Firefox. Trying to disable JavaScript from the Selenium JavaScript runner (which I assume your NUnit tests use under the hood) would result in a security exception, so I'm afraid this method isn't of use to you. mfn's suggestions below are what I'd generally consider these days. Commented Nov 4, 2009 at 12:12

18 Answers 18

24

This is a way to do it if you use WebDriver with FireFox:

FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work with new selenium and firefox. It used to work earlier, but not now.
12

This is the simple answer, for python at least.

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference("javascript.enabled", False);
driver = webdriver.Firefox(profile)

1 Comment

Does not work in 2018, but the following answer does: stackoverflow.com/a/51681608/913569
10

Edit

In the meantime better alternatives did arise, please see the other answers e.g. https://stackoverflow.com/a/7492504/47573 .

Original answer

Other possibilities would be:

  • Write your application to support disabling JavaScript (yes, the web application).
    Sounds crazy? Isn't. In our development process we're doing exactly this, implementing features without JS until all are there, then spice up with JS. We usually provide a hook within all templates which can control from a single point to basically which JS off/on from the web application itself. And, yes, the application is hardly recognizable without JS enabled, but it's the best way to ensure things work properly. We even write Selenium tests for it, for both versions; NOJS and JS. The NOJS are so quickly implemented that they don't matter compared to what it takes to write sophisticated JS tests ...
  • Modify the appropriate browser profile to have JS disabled. I.e. for FF you can tell Selenium which profile to use; you can load this profile normally, disable JS in about:config and feed this profile as default profile to Selenium RC.

3 Comments

Good answer for 2009, not so much for 2018. :)
True, I see if I can delete it. Edit: nope, can't delete accepted answer and can't un-accept it. Edit2: edited answer to reference currently the ones with highest votes. Thx @damn 👍
In 2018 it works like this stackoverflow.com/a/53054654/7685008
7

As of 2018 the above solutions didn't work for me for Firefox 61.0.1 and geckodriver 0.20.1.

And here is a solution which works.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

browser_exe = '/path/to/firefox/exe'
browser_driver_exe = '/path/to/geckodriver/exe'

firefox_binary = FirefoxBinary(browser_exe)

profile = webdriver.FirefoxProfile()
profile.DEFAULT_PREFERENCES['frozen']['javascript.enabled'] = False
profile.set_preference("app.update.auto", False)
profile.set_preference("app.update.enabled", False)
profile.update_preferences()

driver = webdriver.Firefox(
    executable_path=browser_driver_exe,
    firefox_binary=firefox_binary,
    firefox_profile=profile,
)

driver.get("about:config")

Now in the search bar type javascript and you should see the following.

enter image description here

1 Comment

You don't need to fiddle with binary paths, see this answer: stackoverflow.com/a/59227090/4592067
5

The following works as of late 2019 with geckodriver 0.24.0 ( 2019-01-28) and Python 3.6.9 (and possibly other nearby versions.)

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference('javascript.enabled', False)
driver = webdriver.Firefox(options=options)
driver.get('about:config') # now you can filter for javascript.enabled and check

about:config shows false for javascript.enabled. enter image description here

1 Comment

Updated this answer b/c of DeprecationWarning: use options instead of firefox_options
4

The steps to use the script referenced above aren't to bad:

  1. Create the selenium "user-extensions.js" file as mentioned in the article you link.
  2. Select your "user-extensions.js" file in the Selenium preferences in Options->Options.
  3. Use the script by selecting the command "DisableJavascript" or "EnableJavascript" from the command list (or just type it manually).

For screen shot examples of steps 2 and 3 see: http://i32.tinypic.com/161mgcm.jpg

Update: For information about using user-extensions.js with Selenium RC try the following URL: http://seleniumhq.org/docs/08_user_extensions.html

3 Comments

Will this command show up in nunit. I write all my tests in nunit.
I believe so? I have not tried it so I can't be sure. Try the documentation I've added to my answer for information about using user-extensions.js with Selenium RC (which I assume you're using).
"i32.tinypic.com/161mgcm.jpg" where is the image? You should have added code snippet.
2

Sometimes, profile.set_preference("javascript.enabled", False) does not work in Firefox. Use the below python to get around this:

 from pyvirtualdisplay import Display
 from selenium import webdriver
 from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
 from selenium.webdriver.common.keys import Keys
 from selenium.webdriver.common.action_chains import ActionChains


 profile = webdriver.FirefoxProfile()
 profile.update_preferences() #May or may not be needed

 display = Display(visible=0, size=(1200, 800))
 display.start()

 browser = webdriver.Firefox(profile)
 browser.get("about:config")
 actions = ActionChains(browser)
 actions.send_keys(Keys.RETURN)
 actions.send_keys("javascript.enabled")
 actions.perform()
 actions.send_keys(Keys.TAB)
 actions.send_keys(Keys.RETURN)
 actions.send_keys(Keys.F5)
 actions.perform()

 browser.quit()
 display.stop()

2 Comments

Do you have any idea why the set_preference is not working? It's an issue I'm having and I don't want to use this workaround if I can do it 'properly'.
Sorry do not know
2

If you want solution with chrome, chromedriver, Python. This works for any version of chrome, assuming the layout for disabling JS remains same.

from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep

path = 'chrome://settings/content/javascript'
options = webdriver.ChromeOptions()
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get(path)
# clicking toggle button
sleep(1)
ActionChains(chrome_driver).send_keys(Keys.TAB).send_keys(Keys.TAB).send_keys(Keys.ENTER).perform()
driver.get('https://www.google.com/')

1 Comment

As for Chrome 81, you need 5 tabs
2

You can disable javascript using selenium at runtime by using the code:

from selenium import webdriver

options= webdriver.ChromeOptions()

chrome_prefs = {}
options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"javascript": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"javascript": 2}
driver = webdriver.Chrome("your chromedriver path here",options=options)

driver.get('https://google.com/search?q=welcome to python world')

sample_Image

Comments

1

It looks like creating this file will give you the functions that you need to disable javascript for your test. You will call the "doDisableJavascript" function before you begin the test and the "doEnableJavascript" function when you want to enable it again.

2 Comments

how do I call it though? Like where am I calling it from?
with Selenium calling functions that you created would be the name of the function without the do e.g doDisableJavascript would be called from selenium with the command DisableJavascript
1

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.

Comments

1

Set the browser name in the selenium settings to htmlunit.

This is a driver that has JavaScript disabled by default https://code.google.com/p/selenium/wiki/HtmlUnitDriver . The JavaScript required for selenium to interact with the page will still be able to run.

Comments

0

You don't need to disable JavaScript. If you fill out your form you can use JavaScript to submit your form e.g. use runScript and window.document.forms[0].submit().

Whenever you call submit() directly on a form, the form is submitted directly to the server, there is no onsubmit event fired, and therefore, no client-side validation. (Unless your form action is something like javascript:validateForm(), in which case your system doesn't work when JavaScript is disabled).

Comments

0

You can disable the JavaScript during runtime by using the code:

WebDriver driver = new FirefoxDriver();
driver.get("about:config");
Actions act = new Actions(driver);
act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform();
Thread.sleep(1000);
act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).sendKeys(Keys.F5).perform();

Comments

0

I substitute this parameter in the Options () configuration, I don’t remember from whom I spied it (for someone on SO), but it works:

from selenium.webdriver.firefox.options import Options

options = Options()
options.preferences.update({'javascript.enabled': False})
browser = webdriver.Firefox(options=options)

Selenium 3.14 and Firefox 63.0

Comments

0

I was trying to achieve the same in Chrome (in 2020), spent too much time on this, the final solution was to automate the disabling of Javascript in Chrome settings page.

To do that, i was writing a function to get Shadow DOM elements from Chrome with Javascript by the provided path, until i reached the level where the control i had to click on was located.

This is the function i was using for that (i found the solution here and modified a bit):

public IWebElement GetElementFromShadow(string[] Path)
{
    IWebElement root = null;
    foreach (string act in Path)
    {
        if (root == null)
        {
            root = (IWebElement)((IJavaScriptExecutor)_driver).ExecuteScript("return document.querySelector(arguments[0]).shadowRoot", act);
        }
        else
        {
            root = (IWebElement)((IJavaScriptExecutor)_driver).ExecuteScript("return arguments[0].querySelector(arguments[1]).shadowRoot", root, act);
        }
    }
    return root;
}

Then in the test definition, i created an array of strings with the shadow dom path i found in DevTools, used the function above to get the WebElement from inside the nested shadowRoot, so i could click on it with Selenium:

string[] DisableJSShadowRootPath = { "settings-ui", "settings-main", "settings-basic-page", "settings-privacy-page", "category-default-setting", "settings-toggle-button", "cr-toggle" };
IWebElement control = _JSsettings.GetElementFromShadow(DisableJSShadowRootPath);
control.FindElements(By.TagName("span")).Where(e => e.GetAttribute("id") == "knob").First().Click();

The path can be easily found at the bottom ribbon of Chrome DevTools:

enter image description here

Comments

0

This solution applies to Selenium for Python.

In case javascript.enabled doesn't work, you can use selenium-wire to block javascript requests. Note that inline scripts will still run. You can also block other requests like stylesheets and images using the request path's file extension, or the Accept header.

from seleniumwire import webdriver

def interceptor(request):
    if request.path.endswith('.js'):
        request.abort()

driver = webdriver.Firefox()
driver.request_interceptor = interceptor
# driver.get()

selenium-wire docs
selenium-wire on PyPi

Tested using

selenium==4.1.0
selenium-wire==4.6.2

Comments

0

Scrapy-Selenium Middleware:

import time
import logging
from selenium import webdriver
import undetected_chromedriver as uc
from scrapy.http import HtmlResponse
from scrapy_selenium import SeleniumRequest
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException

class SeleniumMiddleware(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        path = 'chrome://settings/content/javascript'
        options = uc.ChromeOptions()
        options.binary_location = "/usr/bin/chromium"
        self.driver = uc.Chrome(headless=False, use_subprocess=False)
        self.driver.maximize_window()
        self.driver.get(path)
        time.sleep(1)
        ActionChains(self.driver).send_keys(Keys.TAB).send_keys(Keys.TAB).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
        self.driver.get('https://www.google.com/')
        self.request_count = 0

    def process_request(self, request, spider):
        self.logger.debug(f"Processing request: {request.url}")
    
        if not isinstance(request, SeleniumRequest):
            return None
    
        try:
            self.driver.get(request.url)

            if self.request_count >= 100:
                self.restart_browser()
            
            if request.wait_until:
                WebDriverWait(self.driver, request.wait_time).until(
                    request.wait_until
                )

            if request.script:
                self.driver.execute_script(request.script)

            body = str.encode(self.driver.page_source)
        
            self.request_count += 1
        
            request.meta.update({'driver': self.driver})
        
            return HtmlResponse(self.driver.current_url, body=body, encoding='utf-8', request=request)
        
        except WebDriverException as e:
            logging.error(f"WebDriverException encountered: {e}")
            self.restart_browser()
            return None

    def restart_browser(self):
        self.driver.quit()
        path = 'chrome://settings/content/javascript'
        options = uc.ChromeOptions()
        options.binary_location = "/usr/bin/chromium"
        self.driver = uc.Chrome(headless=False, use_subprocess=False)
        self.driver.maximize_window()
        self.driver.get(path)
        time.sleep(1)  ActionChains(self.driver).send_keys(Keys.TAB).send_keys(Keys.TAB).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
        self.driver.get('https://www.google.com/')
        self.request_count = 0
    
    def __del__(self):
        self.driver.quit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.