How to disable Javascript when using Selenium?

前端 未结 16 649
星月不相逢
星月不相逢 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:39

    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')
    
    

    Example image here:-https://i.stack.imgur.com/DdKZQ.png

    0 讨论(0)
  • 2020-12-05 05:41

    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/')
    
    0 讨论(0)
  • 2020-12-05 05:42

    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(firefox_options=options)
    driver.get('about:config') # now you can filter for javascript.enabled and check
    

    about:config shows false for javascript.enabled.

    0 讨论(0)
  • 2020-12-05 05:42

    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.

    0 讨论(0)
  • 2020-12-05 05:43

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题