I know I can use Firefox profiles to disable JavaScript. For example, see Enable/disable javascript using Selenium WebDriver.
However, I have a case where I need JavaScript to be enabled in order for me to log in to a page, but then I want JavaScript to be disabled after I am logged in, so that when I do page_source
, it returns the DOM as if JavaScript has not run. The key is that the login page requires JavaScript. Is it possible to dynamically control whether JavaScript is on or off in Selenium WebDriver?
I suggest not to dynamically disable the javascript as most functionality of webdriver is written in javascript and so it may give unexpected results sometimes. Better to use Firefox Profiles to disable it, but I have written the code that may help you disable it during runtime.
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();
If using Selenium IDE, you can refer: http://thom.org.uk/2006/03/12/disabling-javascript-from-selenium/ as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With