How to Turn Off JavaScript Mid-Session in Kameleo

  • Created

When automating browsers with Kameleo, you might need to disable JavaScript for specific URLs during your session. This can be particularly useful for:

  • Bypassing certain types of detection.
  • Managing resource loading.
  • Controlling script execution on specific pages.

Managing JavaScript Execution

The following method allows you to selectively disable JavaScript for specific URLs using Chrome DevTools Protocol (CDP) in conjunction with Selenium. The function automatically re-enables JavaScript after navigation (in the finally block).

  • async function manageJavaScriptExecution(driver, url, disableJs = true) {
        try {
            await driver.sendCdpCommand('Emulation.setScriptExecutionDisabled', { value: disableJs });
            await driver.get(url);
        } finally {
            await driver.sendCdpCommand('Emulation.setScriptExecutionDisabled', { value: false });
        }
    }
  • def manage_javascript_execution(driver, url, disable_js=True):
        try:
            driver.execute_cdp_cmd('Emulation.setScriptExecutionDisabled', {'value': disable_js})
            driver.get(url)
        finally:
            driver.execute_cdp_cmd('Emulation.setScriptExecutionDisabled', {'value': False})

Key Features

  • Uses Chrome DevTools Protocol (CDP) to toggle JavaScript
  • Automatically re-enables JavaScript after navigation (in the finally block)
  • Includes error handling and logging
  • Provides a clean interface for enabling/disabling JavaScript per URL

Was this article helpful?

0 out of 0 found this helpful