13

How can I disable Java Script in Selenium's Chrome Driver using python

Le Truong Sinh
  • 181
  • 1
  • 1
  • 8

4 Answers4

26

It's Really easy ! Just try this code !

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option( "prefs",{'profile.managed_default_content_settings.javascript': 2})
chrome = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
chrome.get('http://stackoverflow.com/')

If you want to disable Images, just replace javascript with image.

Yassine Akermi
  • 470
  • 5
  • 11
  • It works!! Thanks. i've made some tests with headless mode of Chrome, but in this case it doesn't seems to bem available. – rodrigorf Mar 19 '18 at 20:48
7

Disabling JavaScript in Chrome is possible with old ChromeDriver prior to ChromeDriver2, which only supports Chrome 28 or under. try as below :-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-javascript")
driver = webdriver.Chrome(chrome_options=chrome_options)

WARNING: Running without JavaScript is unsupported and will likely break a large portion of the ChromeDriver's functionality. I suspect you will be able to do little more than navigate to a page. This is NOT a supported use case, and we will not be supporting it.

Hope it will help you...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
2

If you're using Nightwatch.js, the configuration you have to use is

var CHROME_CONFIGURATION = {
    browserName: 'chrome',
    "chromeOptions" : {
        "prefs" : {
            'profile.managed_default_content_settings.javascript': 2
        }
    }
};
Joviano Dias
  • 1,043
  • 10
  • 13
1

It's really difficult. You can try doing this way:

DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability("chrome.switches", Arrays.asList("--disable-javascript"));

But as it is written here, you can´t disable JavaScript if you use ChromeDriver2.

user565447
  • 969
  • 3
  • 14
  • 29
  • This did not work for me in Java. But I managed to do it with `ChromeDriver 2.41.578706` like this: https://stackoverflow.com/a/57316009/1545579 – Mahdi Aug 01 '19 at 19:43