How to disable Javascript when using Selenium?
Asked Answered
S

18

28

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/

Sharma answered 17/8, 2009 at 2:17 Comment(1)
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. – Jonniejonny
I
10

Edit

In the meantime better alternatives did arise, please see the other answers e.g. https://mcmap.net/q/487987/-how-to-disable-javascript-when-using-selenium .

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.
Inhaler answered 22/8, 2009 at 20:15 Comment(3)
Good answer for 2009, not so much for 2018. :) – Enyedy
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 πŸ‘ – Inhaler
In 2018 it works like this https://mcmap.net/q/487987/-how-to-disable-javascript-when-using-selenium – Gon
C
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);
Counterstamp answered 20/9, 2011 at 22:21 Comment(1)
This doesn't work with new selenium and firefox. It used to work earlier, but not now. – Acea
D
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)
Dulciana answered 12/3, 2013 at 14:46 Comment(1)
Does not work in 2018, but the following answer does: https://mcmap.net/q/487987/-how-to-disable-javascript-when-using-selenium – Enyedy
I
10

Edit

In the meantime better alternatives did arise, please see the other answers e.g. https://mcmap.net/q/487987/-how-to-disable-javascript-when-using-selenium .

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.
Inhaler answered 22/8, 2009 at 20:15 Comment(3)
Good answer for 2009, not so much for 2018. :) – Enyedy
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 πŸ‘ – Inhaler
In 2018 it works like this https://mcmap.net/q/487987/-how-to-disable-javascript-when-using-selenium – Gon
P
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

Phylys answered 4/8, 2018 at 1:1 Comment(1)
You don't need to fiddle with binary paths, see this answer: https://mcmap.net/q/487987/-how-to-disable-javascript-when-using-selenium – Shafting
C
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

Cryan answered 17/8, 2009 at 13:15 Comment(3)
Will this command show up in nunit. I write all my tests in nunit. – Sharma
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). – Cryan
"i32.tinypic.com/161mgcm.jpg" where is the image? You should have added code snippet. – Forefend
S
4

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

Shafting answered 7/12, 2019 at 14:40 Comment(1)
Updated this answer b/c of DeprecationWarning: use options instead of firefox_options – Shafting
A
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()
Aneto answered 22/4, 2016 at 1:9 Comment(2)
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'. – Wasson
Sorry do not know – Aneto
M
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/')
Metry answered 1/4, 2019 at 11:1 Comment(1)
As for Chrome 81, you need 5 tabs – Perishable
H
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

Hardened answered 2/12, 2020 at 8:40 Comment(0)
V
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.

Vauntcourier answered 17/8, 2009 at 2:28 Comment(2)
how do I call it though? Like where am I calling it from? – Sharma
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 – Belldame
I
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.

Instate answered 29/4, 2011 at 16:23 Comment(0)
B
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.

Brey answered 28/9, 2015 at 15:14 Comment(0)
C
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).

Clymer answered 10/7, 2012 at 9:20 Comment(0)
C
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();
Community answered 20/6, 2015 at 6:19 Comment(0)
G
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

Gon answered 29/10, 2018 at 22:13 Comment(0)
P
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

Provide answered 31/7, 2020 at 10:55 Comment(0)
L
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
Lighthouse answered 2/3, 2022 at 16:3 Comment(0)
A
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()
Alleyway answered 13/2 at 6:35 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.