Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

渲染html后截图,html中的小图标无法生成 #178

Open
jack-baitao opened this issue Dec 25, 2024 · 5 comments
Open

渲染html后截图,html中的小图标无法生成 #178

jack-baitao opened this issue Dec 25, 2024 · 5 comments

Comments

@jack-baitao
Copy link

例如:
.icon-home {
background-image: url('https://cdn-icons-png.flaticon.com/512/3222/3222434.png');
}

在本地浏览器打开html可以展示该图标,但是在linux服务器上渲染,图标是缺失的,这个是怎么回事?

@fanyong920
Copy link
Owner

那应该是图标还没加载出来

@jack-baitao
Copy link
Author

那应该是图标还没加载出来

那该怎么操作一下,我sleep 2 秒也不行

@fanyong920
Copy link
Owner

Browser browser = Puppeteer.launch(launchOptions);
        //打开一个页面
        Page page = browser.newPage();
        page.on(PageEvents.Console, (Consumer<ConsoleMessage>) message -> System.out.println("console: "+message.text()));
        WaitForOptions waitForOptions = new WaitForOptions();
        waitForOptions.setWaitUntil(Collections.singletonList(PuppeteerLifeCycle.networkIdle));

        page.setContent(" <!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>Icon Home Example</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h1>Home Icon Example</h1>\n" +
                "    <img id=\"icon-home\" src='https://cdn-icons-png.flaticon.com/512/3222/3222434.png'></img>\n" +
                "</body>\n" +
                "</html>", waitForOptions);
        WaitForSelectorOptions options = new WaitForSelectorOptions();
        options.setVisible(true);
        ElementHandle elementHandle = page.waitForSelector("#icon-home",options);
        while (true){
            Object complete = elementHandle.evaluate("(element) => {\n" +
                    "    return element.complete\n" +
                    "}");
            if((boolean)complete){
                break;
            }
        }

        ElementScreenshotOptions screenshotOptions = new ElementScreenshotOptions();
        screenshotOptions.setFromSurface(true);
        screenshotOptions.setPath("图片.png");
        screenshotOptions.setFullPage(true);
        page.screenshot(screenshotOptions);
        browser.close();

尝试一下这个方法

@fanyong920
Copy link
Owner

//等待图片加载完毕
    @Test
    public void test10() throws Exception {
        launchOptions.setDevtools(true);
        Browser browser = Puppeteer.launch(launchOptions);
        //打开一个页面
        Page page = browser.newPage();
        page.on(PageEvents.Console, (Consumer<ConsoleMessage>) message -> System.out.println("console: " + message.text()));

        page.setContent(" <!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>Icon Home Example</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h1>Home Icon Example</h1>\n" +
                "    <img id=\"icon-home\" src='https://cdn-icons-png.flaticon.com/512/3222/3222434.png'></img>\n" +
                "</body>\n" +
                "</html>");
        WaitForSelectorOptions options = new WaitForSelectorOptions();
        options.setVisible(true);
        ElementHandle elementHandle = page.waitForSelector("#icon-home", options);
        JSHandle jsHandle = page.waitForFunction("(element) => {\n" +
                "    return element.complete;\n" +
                "}", elementHandle);
        System.out.println("图片加载完毕" + jsHandle);
        ElementScreenshotOptions screenshotOptions = new ElementScreenshotOptions();
        screenshotOptions.setFromSurface(true);
        screenshotOptions.setPath("图片.png");
        screenshotOptions.setFullPage(true);
        page.screenshot(screenshotOptions);
        browser.close();
    }

@jack-baitao
Copy link
Author

好的,我先试一下是不是因为网络延迟的原因吧。
实际上我的html是大模型生成的,图标不一定是什么,使用page.waitForSelector("#icon-home", options); 不适用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants