How to disable JavaScript in browser using Selenium (Java)?

前端 未结 6 1648
抹茶落季
抹茶落季 2020-12-20 08:01

In my feature automation, I need to disable JavaScript in browser and run the flow. How to disable JavaScript?

Tried DesiredCapabilities for firefox and Chrome.

相关标签:
6条回答
  • 2020-12-20 08:14

    I don't know Java, but maybe a solution for Python 3 will help you.

    in Python, you can use Options() instead of FirefoxProfile() to deactivate JavaScript:

    from selenium.webdriver.firefox.options import Options
    options = Options()
    options.preferences.update({"javascript.enabled": False})
    driver = webdriver.Firefox(options=options)
    driver.get('about:config')
    

    Maybe Java this:

    FirefoxOptions options = new FirefoxOptions();
    options.preferences.update({"javascript.enabled": False});
    WebDriver driver = new FirefoxDriver(options);
    driver.get('about:config')
    
    0 讨论(0)
  • 2020-12-20 08:15

    this works:

    FirefoxOptions options = new FirefoxOptions();
    options.addPreference("javascript.enabled", false);

    0 讨论(0)
  • 2020-12-20 08:18

    This is how you can do it for Chrome in Java.

            // import org.openqa.selenium.chrome.ChromeOptions;
    
            ChromeOptions options = new ChromeOptions();
            options.addArguments("user-agent=\"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"");
            HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
            chromePrefs.put("profile.default_content_setting_values.javascript", 2);
            options.setExperimentalOption("prefs", chromePrefs);
            new ChromeDriver(options);
    

    And it worked for me with ChromeDriver 2.41.578706. As a bonus I am also setting Googlebot as user-agent.

    In case you need to do something with DesiredCapabilities you can also convert the options above to capabilities:

            // import static org.openqa.selenium.chrome.ChromeOptions.CAPABILITY;
    
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setCapability(CAPABILITY, options);
            new ChromeDriver(capabilities);
    
    0 讨论(0)
  • 2020-12-20 08:20

    As per Selenium 3.6 Java Client Release, the easiest way to disable Javascript in the browser would be to set the setJavascriptEnabled argument through DesiredCapabilities to False and merge it with FirefoxOptions as follows:

    package demo;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class Q46883024_setJavascriptEnabled 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            DesiredCapabilities dc = new DesiredCapabilities();
            dc.setJavascriptEnabled(false);
            FirefoxOptions op = new FirefoxOptions();
            op.merge(dc);
            WebDriver driver = new FirefoxDriver(op);
            driver.get("https://google.com");
            driver.quit();
        }
    }
    

    While execution, the browser you are using may override the setJavascriptEnabled settings.

    0 讨论(0)
  • 2020-12-20 08:23

    Truse me this was random trial but works perfectly for me

    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-20 08:32

    You change the preference value using profile with lots of options:

    DesiredCapabilities capabilities = new DesiredCapabilities();
    // setCapability(SUPPORTS_JAVASCRIPT, javascriptEnabled);
    capabilities.setJavascriptEnabled(false);
    
    FirefoxBinary binary = new FirefoxBinary( new File( binaryPath ) );
    FirefoxProfile profile = new FirefoxProfile();
    
    //profile.setPreference("preferenceName", "Value");
    profile.setPreference("javascript.enabled", false);
    
    RemoteWebDriver driver = new FirefoxDriver(binary, profile, capabilities);
    

    To view the preferences, you can visit the URL about:config

    @See

    • Chrome driver to disable JavaScript issue
    • chromium-command-line-switches
    0 讨论(0)
提交回复
热议问题