How disable debuger on chrome webdriver in Java?

时光毁灭记忆、已成空白 提交于 2020-01-16 08:53:10

问题


My target application use <script type="text/javascript">debugger;</script> So my page is blocked by break point.

How disable debugger on chrome webdriver in Java?

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;

public class Bot {

    public static void main(String[] args) {
        final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
        String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
                SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
        System.setProperty("webdriver.chrome.driver", pathWebdriver);

        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");

    }

}

I think, add javascipt code (revert of debugger;) by this:

((JavascriptExecutor) driver).executeScript("...");

EDIT WITH HEADLESS MODE + SCREENSHOT:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;

public class Bot {

    public static void main(String[] args) throws IOException {
        final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
        String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
                SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
        System.setProperty("webdriver.chrome.driver", pathWebdriver);

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

        final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        FileUtils.forceMkdir(new File(System.getProperty("user.dir") + File.separator + "downloadFiles"));
        FileUtils.writeByteArrayToFile(new File(System.getProperty("user.dir") + File.separator + "downloadFiles" + File.separator + "bot.jpg"), screenshot);
    }

}

The result is same, my sceenshot is white.

来源:https://stackoverflow.com/questions/56682047/how-disable-debuger-on-chrome-webdriver-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!