w3c / w3c/webdriver

Whether to print headers or footers when printing in Chrome.

Open
#1,761 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Print Page
Dominant language
HTML
Stars
718
Forks
228
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the WebDriver print command and the Selenium PrintOptions documentation cited in the issue, then compare the supported fields with Chrome's header and footer behavior. The Selenium issue provides related context. Done would require a clarified WebDriver API decision for displayHeaderFooter, headerTemplate, and footerTemplate, rather than an application-level workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.