How to disable Javascript when using Selenium?

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

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

    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);
    
    0 讨论(0)
  • 2020-12-05 05:31

    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();
    
    0 讨论(0)
  • 2020-12-05 05:36

    Edit

    In the meantime better alternatives did arise, please see the other answers e.g. https://stackoverflow.com/a/7492504/47573 .

    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.
    0 讨论(0)
  • 2020-12-05 05:38

    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

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

    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

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