Selenium WebDriver (Firefox): dynamically disable Javascript

I know that 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 so that I can enter the page, but then I want JavaScript to be disabled after my login, so when I do page_source it returns the DOM as if JavaScript did not start. The key is that the login page requires JavaScript. Is it possible to dynamically control whether JavaScript is enabled or disabled in Selenium WebDriver?

+6
source share
1 answer

I suggest not dynamically disabling javascript, since most webdriver functions are written in javascript, and therefore it can sometimes give unexpected results. It's better to use Firefox Profiles to disable it, but I wrote code that can help you disable it at 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 you are using the Selenium IDE, you can link to http://thom.org.uk/2006/03/12/disabling-javascript-from-selenium/ .

+2
source

Source: https://habr.com/ru/post/989426/


All Articles