SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.42k stars 8.15k forks source link

[🐛 Bug]: Failed to get pdf file where setting the pref to headless #14040

Closed loseangeltyj closed 4 months ago

loseangeltyj commented 4 months ago

What happened?

I used selenium in chrome to output the pdf of the page.It works when not setting headless. But when I add the chrome_options.add_argument('--headless') # 无头模式 Its not working.No output

How can we reproduce the issue?

height_microns = 29700
    width_microns = 210000
    if page_type == 'A2':
        height_microns = 594000
        width_microns = 420000
    chrome_options = uc.ChromeOptions()
    settings = {
        "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isHeaderFooterEnabled": True,
        "pageSize": page_type,
        # # "customMargins": {},
        # "marginsType": 2,  # 边距(2是最小值、0是默认)
        # "scaling": 70,
        # "scalingType": 3,
        # "scalingTypePdf": 3,
        # # "isLandscapeEnabled": True,  # 若不设置该参数,默认值为纵向
        "isCssBackgroundEnabled": True,
        "mediaSize": {
            "height_microns": height_microns,
            "imageable_area_bottom_microns": 0,
            "imageable_area_left_microns": 0,
            "imageable_area_right_microns": width_microns,
            "imageable_area_top_microns": height_microns,
            "name": "ISO_"+page_type,
            "width_microns": width_microns,
            "custom_display_name": page_type
        }
    }
    #
    chrome_options.add_argument('--enable-print-browser')
    # # chrome_options.add_argument('--headless') #headless模式下,浏览器窗口不可见,可提高效率
    prefs = {
        'printing.print_preview_sticky_settings.appState': json.dumps(settings),
        'savefile.default_directory': path_name,
        "profile.default_content_setting_values.automatic_downloads": 1
    }
    chrome_options.add_argument('--kiosk-printing')  # 静默打印,无需用户点击打印页面的确定按钮
    chrome_options.add_argument("--disable-popup-blocking") # 禁用弹出式窗口拦截
    chrome_options.add_argument("--disable-notifications") # 禁用弹出式窗口拦截
    # chrome_options.add_argument('--disable-extensions')  # 禁用扩展
    chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')  # 禁用GPU渲染
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    # chrome_options.add_argument('--disable-features=EnableEphemeralFlashPermission')
    # chrome_options.add_argument('--disable-features=AutofillServerCommunication')
    # chrome_options.add_argument('--disable-remote-fonts')
    # chrome_options.add_argument('--disable-hang-monitor')
    # chrome_options.add_argument('--disable-prompt-on-repost')
    # chrome_options.add_argument('--disable-background-networking')
    # chrome_options.add_argument('--disable-sync')
    # chrome_options.add_argument('--disable-translate')
    # chrome_options.add_argument('--metrics-recording-only')
    # chrome_options.add_argument('--safebrowsing-disable-auto-update')
    # chrome_options.add_argument('--password-store=basic')
    # chrome_options.add_argument('--use-mock-keychain')
    # chrome_options.add_argument('--proxy-server=http://{}:{}'.format('47.113.224.182','59394') )
    chrome_options.add_experimental_option('prefs', prefs)
    # workdir, port = get_driver_info()
    # chrome_options.add_argument("--user-data-dir={}".format(workdir))
    chrome_options.add_argument("--user-data-dir={}".format(r'D:\workdir1'))
    # chrome_options.add_argument("--remote-debugging-port={}".format(port))

    driver = uc.Chrome(use_subprocess=True,
                       options=chrome_options,
                       patcher_force_close=True,
                       driver_executable_path=path)

Relevant log output

2024-05-26 16:46:59.758 | INFO     | ExecutorHandler:output_png:173 - output_png fileName:D:/digao/190/
2024-05-26 16:47:08.973 | INFO     | ExecutorHandler:output_pdf:258 - output_pdf fileName: XXX.pdf

Operating System

Windows

Selenium version

python

What are the browser(s) and version(s) where you see this issue?

chrome 125

What are the browser driver(s) and version(s) where you see this issue?

chrome driver win 64 125.0.6422.78

Are you using Selenium Grid?

No response

github-actions[bot] commented 4 months ago

@loseangeltyj, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] commented 4 months ago

Hi, @loseangeltyj. This issue has been determined to require fixes in ChromeDriver.

You can see if the feature is passing in the Web Platform Tests.

If it is something new, please create an issue with the ChromeDriver team. Feel free to comment the issues that you raise back in this issue. Thank you.

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.