'How to disable Javascript when using Selenium?
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/
Solution 1:[1]
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);
Solution 2:[2]
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)
Solution 3:[3]
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.
Solution 4:[4]
The steps to use the script referenced above aren't to bad:
- Create the selenium "user-extensions.js" file as mentioned in the article you link.
- Select your "user-extensions.js" file in the Selenium preferences in Options->Options.
- 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
Solution 5:[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
Solution 6:[6]
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()
Solution 7:[7]
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')
Solution 8:[8]
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.
Solution 9:[9]
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.
Solution 10:[10]
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.
Solution 11:[11]
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/')
Solution 12:[12]
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).
Solution 13:[13]
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();
Solution 14:[14]
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
Solution 15:[15]
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:
Solution 16:[16]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow




