Java Selenium DevTools v127

问题描述 投票:0回答:1

目前我正在使用 selenium 在 web scraper 上使用 java,我想在加载任何网站之前添加 javascript 代码,以便我可以覆盖一些方法...我想使用 DevTools,但不幸的是我对 addScriptToEvaluateOnNewDocument 没有任何效果。 ..但是这段代码以某种方式工作,所以我假设 DevTools 依赖项是正确的。

devTools.send(Emulation.setDeviceMetricsOverride(
                500,  // width
                600,  // height
                50,   // deviceScaleFactor
                true,  // mobile
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty()
            ));

完整测试代码:

String driverpath = Main.configuration.getString("browser.chromedriver");
            String browserpath = Main.configuration.getString("browser.chromebrowser");

            if(driverpath != null && !driverpath.isBlank()){
                System.setProperty(
                    "webdriver.chrome.driver",
                    driverpath
                );
                System.setProperty(
                "webdriver.gecko.driver",
                driverpath
                );
            }
                
            ChromeOptions options = new ChromeOptions();

            if(headless){
                Main.printf("Chrome headless");
                options.addArguments("--headless");
            }
            options.addArguments("--disable-search-engine-choice-screen");
            options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
            options.addArguments("start-maximized"); // open Browser in maximized mode
            options.addArguments("disable-infobars"); // disabling infobars
            options.addArguments("--disable-extensions"); // disabling extensions
            options.addArguments("--disable-gpu"); // applicable to windows os only
            options.addArguments("--no-sandbox"); // Bypass OS security model
            if(browserpath != null && !browserpath.isBlank()){
                options.setBinary(browserpath);
            }

            Main.printf("Start Chromedriver...");
            this.driver = new ChromeDriver(options);


            DevTools devTools = ((HasDevTools) driver).getDevTools();


            devTools.createSession();

            
            devTools.send(Emulation.setDeviceMetricsOverride(
                500,  // width
                600,  // height
                50,   // deviceScaleFactor
                true,  // mobile
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty()
            ));
            

            String script = "window.was_here=true; console.log('Script executed');";

            ScriptIdentifier identifier = devTools
                    .send(Page.addScriptToEvaluateOnNewDocument(script, // source
                            Optional.empty(), // worldName
                            Optional.of(false), // includeCommandLineAPI
                            Optional.of(false) // runImmediately
            ));
            System.err.println(String.format(
                    "Method Page.addScriptToEvaluateOnNewDocument result: %s", identifier));

            driver.navigate().to("https://intoli.com/blog/not-possible-to-block-chrome-headless/chrome-headless-test.html");
            try {
                Thread.sleep(100); // nur zum Test, für sichtbare Effekte
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Object d = getJSExecutor().executeScript("return window.was_here");
            if(d != null){
                boolean data = (Boolean) d;
                Main.printf("Scraper", "DATA:"+(data ? "T" : "F"));
            }else Main.printf("Scraper", "D IS NULL!!");

            devTools.send(Page.removeScriptToEvaluateOnNewDocument(identifier));

            try {
                Thread.sleep(500000); // nur zum Test, für sichtbare Effekte
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            driver.quit();
            System.exit(0);

            Main.printf("Chromedriver started :)");

Maven Selenium 依赖项:

  • selenium-java:4.25.0
  • selenium-devtools-v127:4.25.0

Chrome浏览器:GoogleChromePortable64_127.0.6533.120_online
Chrome 驱动程序:v127 (https://datatoolbar.com/downloads/Drivers/chrome.127/)

任何人都可以告诉我为什么我的测试代码不起作用,我从这个项目

复制了测试
java selenium-webdriver devtools
1个回答
0
投票

在没有 DevTools 的情况下搜索几个小时后的解决方案:

HasCdp cdp = ((HasCdp)driver);
Map map = new HashMap<String,String>();
map.put("source","console.log('test');");
cdp.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", map);
© www.soinside.com 2019 - 2024. All rights reserved.