3

I have a task: go to the page, find on it a link to another page and go to it. I need to load another page without javascript.

If I use this method:

options = Options()
options.add_experimental_option("prefs", {
    "profile.managed_default_content_settings.javascript": 2
})

This disables javascript on all pages, which is unacceptable. Creating two different driver instances and switching between them takes too much time and resources.

Any ideas how to disable javascript on a specific page?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
kshnkvn
  • 876
  • 2
  • 18
  • 31

3 Answers3

2

I will provide my solution in Java. There is always an option to simulate toggling javascript on/off in the chrome settings. The trick is to create a custom WebDriver class that implements WebDriver.

To begin, you could save current page URL so the driver can know where to come back after toggling javascript, afterwards you should keep track about javascript current state - and if current javascript state differentiates from passed enable argument then proceed with toggling javascript. The button for toggling javascript works on the middle of screen with radius of at least 60% of the page, so the X axis will always work correctly, while Y axis is an approximation which should work most of the time.

Here is the implementation:

public class IWebDriver implements WebDriver {

     private boolean enableJavascript;

     private WebDriver driver;

     public IWebDriver() {
         // Javascript is enabled by default
         this.enableJavascript = true;
         this.driver = new ChromeDriver();
     }

     public void setEnableJavaScript(boolean enable) throws Exception {
         /* Save current page URL */
         String currentUrl = getCurrentUrl();

         /* Toggle javascript if passed argument javascript state differentiates from actual javascript state */
         if (this.enableJavaScript != enable) {
             this.driver.navigate().to("chrome://settings/content/javascript");
             Dimension d = driver.manage().window().getSize();
             int x = d.getWidth() / 2;
             int y = (int) ((float) d.getHeight() * 0.123f);
             Actions actions = new Actions(this.driver);
             actions.moveByOffset(x, y).click().build().perform();
        
             // change current javascript enabled state
             this.enableJavaScript = !this.enableJavaScript;
         }

         /* Get back to the original URL */
         this.driver.navigate().to(currentUrl);
     }

     public boolean isEnableJavascript() {
         return this.enableJavascript;
     }

     //Getters, setters and overridden methods...

 }
BrunoT
  • 414
  • 4
  • 9
1

When you initiate a new ChromeDriver to initiate a new Chrome browser process you have to configure the instance of the ChromeDriver with ChromeOptions right at the begining which remains unchanged throughout the lifetime of the ChromeDriver and remains uneditable while it drives the Chrome browser process.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies and other session attributes from the initiated ChromeDriver capabilities or from the Browsing Session , still you won't be able to change those attributes.

So instead of disabling javascript on all the pages, if your usecase is to disable javascript on some particular pages you may need to execute those tests with a seperate configuration disabling the javascript.

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.

You can find a relevant discussion in Change ChromeOptions in an existing webdriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I like the Solution that was provided by BrunoT,

I have one improvement. Instead of using the mouse, tabs are a bit more predictable so you don't have to worry about the positioniong. Here is some code in C#

    public void ToggleJavascript()
    {
        driver.Navigate().GoToUrl("chrome://settings/content/javascript");
        new Actions(driver)
            .SendKeys("\t")
            .Pause(TimeSpan.FromMilliseconds(250))
            .SendKeys("\t")
            .Pause(TimeSpan.FromMilliseconds(250))
            .SendKeys(Keys.ArrowDown)
            .Perform();
    }
Alex Begun
  • 451
  • 3
  • 7