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
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)
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);
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();
In the meantime better alternatives did arise, please see the other answers e.g. https://stackoverflow.com/a/7492504/47573 .
Other possibilities would be:
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
The steps to use the script referenced above aren't to bad:
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