Stéphane GRILLON
Stéphane GRILLON

Reputation: 11882

How to disable all breakpoint on selenium Webdriver with Java?

I want disable all breakpoint (come from debugger; javascript target app) on selenium Webdriver with Java. My target app run a breakpoint every x seconds and stop my selenium run.

enter image description here

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");

options.addArguments("--disable-  ???? ");     <= ???????

WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");

you can find all chome option here but I do not find this feature.

EDIT:

I try this (by Javascript via JavascriptExecutor) but do not work:

((JavascriptExecutor) driver).executeScript("debugger; = function(){};");

or

((JavascriptExecutor) driver).executeScript("debugger = function(){};");

or

((JavascriptExecutor) driver).executeScript("window.debugger = function(){};");

EDIT 2:

I try fix this issue by javascript but do not work:

stackoverflow: How to disable debugger; command in JS?

debugger is not a JavaScript member, it can't be overridden.

EDIT 3:

in the meantime, I asked a new issue on the selenium library here

Upvotes: 2

Views: 1018

Answers (2)

Pavel
Pavel

Reputation: 1

I found partial solution with event Debugger.paused https://chromedevtools.github.io/devtools-protocol/tot/Debugger/#event-paused/.

I've created a chrome extension and load it when start the browser in selenium.

Manifest.json

{
    "name": "Plugin",
    "short_name": "Plugin",
    "version": "0.1",
    "description": "Plugin",
    "author": "plugin",
    "devtools_page": "devtools.html",
    "permissions": [
        "debugger",
        "tabs",
        "storage"
    ],
    "host_permissions": [
        "http://*/*",
        "https://*/*"
    ],
    "manifest_version": 3
}

devtools.html

<html>
<head>
<meta charset="utf-8" /> 
</head>
<body>
<script src="devtools.js">
</script>
</body>
</html>

devtools.js

let debugee = null;
function initHandling(targetTab) {
    debugee = {
        tabId: targetTab.id
    };
    chrome.debugger.attach(debugee, "1.3", () => {
        chrome.debugger.sendCommand(debugee, "Fetch.enable", {
            patterns: [
                {
                    resourceType: 'Script'
                },
                {
                    resourceType: 'Document'
                }
            ]
        });
        chrome.debugger.sendCommand(debugee, "Debugger.enable");
        chrome.debugger.sendCommand(debugee, "Debugger.setBreakpointsActive", {
            'active': false
        });
    });
    chrome.debugger.onEvent.addListener((source, method, params) => {
        // The event triggers when javascript meet "debugger" command
        if (source.tabId !== targetTab.id) return;
        if (method === "Debugger.paused") {
            // Tell chrome "resume" the breakpoint
            chrome.debugger.sendCommand(debugee, 'Debugger.resume');
        }
    });
}
let queryOptions = {
    active: true
};
chrome.tabs.query(queryOptions, (tabs) => {
    initHandling(tabs[0]);
});

You need create crx package of the extention. I use this lib https://www.npmjs.com/package/crx and this command crx pack source/. Then add it to selenium chrome instance as base64 string using ChromeOptions.addEncodedExtensions(string).

But sometimes, some sites have protection for debuging and call debugger 10000+ times. And this solution works slowly.

I tried to set Debugger.setBreakpointsActive to false https://chromedevtools.github.io/devtools-protocol/tot/Debugger/#method-setBreakpointsActive, but it doesn't work when browser execute code like this eval('deb'+'ugger').

Upvotes: 0

g.annunziata
g.annunziata

Reputation: 3276

Seems good idea to call script to redefine debugger, but I think you can try this:

((JavascriptExecutor) driver).executeScript("debugger = function(){};");

or:

((JavascriptExecutor) driver).executeScript("window.debugger = function(){};");

Upvotes: 0

Related Questions