Search code examples
google-chromeselenium-webdriverchromium

The Chromium headless page is scaled, and the resolution is not as expected


I am using Selenium + Chromium headless for automated testing, and the execution environment has been encapsulated into a Docker image based on Debian. Through screenshots taken during the execution, I noticed that the page is being scaled down. I set the resolution to 1920×1080, and while the page meets this resolution, the elements appear to be shrunk, with large white margins on both sides. Here is an example of a normal webpage: an example of a normal webpage And here is the screenshot taken by Selenium: the screenshot taken by Selenium

My chromium variables is :

--headless
--window-size= 1920,1080
--force-device-scale-factor=1
--disable-gpu
--no-sandbox
--hide-scrollbars
--disable-dev-shm-usage
--remote-allow-origins=*

My docker file likes:

FROM debian:bookworm-slim
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        chromium \
        libglib2.0-0 \
        libnss3-dev \
        libxcb1 \
        locales \
        openjdk-17-jre \
        fontconfig \
        fonts-ipafont-gothic \
        fonts-wqy-zenhei \
        fonts-thai-tlwg \
        fonts-kacst \
        fonts-symbola \
        fonts-noto \
        fonts-freefont-ttf


Please tell me what I should do next. Thank you.


Solution

  • Try this to set resolution

    driver.set_window_size(1920, 1080) 
    
    
    driver.save_screenshot("file_name.png")