w3c / webdriver

Remote control interface that enables introspection and control of user agents.
https://w3c.github.io/webdriver/
Other
676 stars 190 forks source link

Whether to print headers or footers when printing in Chrome. #1761

Open jaychoubaby opened 9 months ago

jaychoubaby commented 9 months ago

I am using selenium to drive chrome, rendering HTML pages into PDF, and I have succeeded. However, I found that I cannot set the styles for headers and footers, nor can I control their visibility. But when operating with chrome in the browser, these functions are indeed available. Since I can use the method webDriver.print(new PrintOptions()).getContent(); in selenium to set parameters such as orientation, scale, background, shrinkToFit, pageSize, pageMargin, and pageRanges through PrintOptions(), why can't other properties be set together? After checking the source code and information, I found that it is not supported and raised an issue on the selenium project. The response was that this requires webdriver support. image It seems that everything is supported except for setting the header and footer. The following are the issues I raised in Selenium: https://github.com/SeleniumHQ/selenium/issues/12763#issuecomment-1724774701

The following is the information I have looked up: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/print/PrintOptions.html image I have reviewed the source code and documentation, but it seems that I cannot find a way to set --print-header-template in PrintOptions. According to my search on Python methods, it appears that this can only be set in ChromeOptions. Is there a better way for me to set this property in PrintOptions or RemoteWebDriver?

Also, I have another question. Currently, I am using springboot integrated with selenium. Here is my code:

@Data
public class CpPrintOptions extends PrintOptions {

    public Map<String, Object> toMap() {
        final Map<String, Object> options = new HashMap<>(7);
        options.put("page", getPageSize());
        options.put("orientation", getOrientation().toString());
        options.put("scale", getScale());
        options.put("shrinkToFit", getShrinkToFit());
        options.put("background", getBackground());
        final String[] effectivePageRanges = getPageRanges();
        if (effectivePageRanges != null) {
            options.put("pageRanges", effectivePageRanges);
        }
        options.put("margin", getPageMargin());
        options.put("displayHeaderFooter", true);
        options.put("headerTemplate", "<span style=\"font-size:8px; margin-left: 5px\">Page <span class=pageNumber></span> of <span class=totalPages></span></span>");
        options.put("footerTemplate", "<span style=\"font-size:8px; margin-left: 5px\">Generated on {datetime.now().strftime(\"%m/%d/%Y at %H:%M\")} - url = <span class=url></span></span>");
        return options;
    }
}

I tried to integrate PrintOptions() and specify the properties, but it seems not to work.