seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5.2k stars 963 forks source link

Headless chrome docker - maximized window not working #1229

Closed sridharaiyer closed 2 years ago

sridharaiyer commented 2 years ago

Hi Tried all this but was not able to get the browser in maximized state in headless running inside docker

class ITests(BaseCase):
    def load_driver(self):
        """
        This function Add custom chrome browser capabilities and to handle PDF File download functionalities
        """
        self.driver.quit()
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--use-fake-ui-for-media-stream")
        chrome_options.add_argument("--kiosk-printing")
        chrome_options.add_argument("--kiosk")
        chrome_options.add_argument("--start-maximized")
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--window-size=1920x1080")
        chrome_options.add_experimental_option("prefs", prefs)
        chrome_options.add_experimental_option(
            "excludeSwitches",
            ["enable-automation", "enable-logging", "enable-blink-features"])
        self.driver = webdriver.Chrome(options=chrome_options)

A step is failing due to the screen not maximized as the elements in a table are getting out of the view. I don't want to use the javascript scroll into view as I want the application to be automated when in a maximized state.

mdmintz commented 2 years ago

@sridharaiyer In your code, you seem to be setting both --start-maximized and --window-size, but those would likely conflict because you can’t set both of them at the same time. Also, there’s an existing Dockerfile: https://github.com/seleniumbase/SeleniumBase/blob/master/Dockerfile. And you can set maximized windows for tests from the command-line with “--maximized”.

sridharaiyer commented 2 years ago

Hi, I should have clarified more. So here you go -

Here is my selenium and Seleniumbase version selenium==3.141.0 seleniumbase==1.63.9

The issue is, even before I run in Docker, the test is failing even in local headless execution.

Yes, I am using the docker file taken from your repository, with a few additions. Here is my dockerfile.

# SeleniumBase Docker Image
FROM ubuntu:18.04

#=====================================================================================================================
#Install Oracle instant client SDK, Bash Command Line Tools, Java 8, Python and Basic Python Tools
#=====================================================================================================================
ENV TZ=America/Chicago
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false update && \
    apt-get install -y software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt-get update && \
    apt-get -qy --no-install-recommends install -y sudo curl libxi6 libgconf-2-4 vim xvfb python3.9 python3.9-distutils python3-pip python3-setuptools python3-dev python-distribute openjdk-8-jdk ant ca-certificates-java wget unzip libaio1 && \
    alias python=python3 && \
    echo "alias python=python3" >> ~/.bashrc && \
    rm /usr/bin/python3 && \
    ln -s python3.9 /usr/bin/python3 && \
    wget https://download.oracle.com/otn_software/linux/instantclient/211000/instantclient-sdk-linux.x64-21.1.0.0.0.zip && \
    wget https://download.oracle.com/otn_software/linux/instantclient/211000/instantclient-basic-linux.x64-21.1.0.0.0.zip && \
    mkdir /opt/oracle && \
    unzip instantclient-basic-linux.x64-21.1.0.0.0.zip -d /opt/oracle && \
    unzip instantclient-sdk-linux.x64-21.1.0.0.0.zip -d /opt/oracle && \
    pwd && ls /opt/oracle && \
    mv /opt/oracle/instantclient_21_1 /opt/oracle/instantclient && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get clean && \
    update-ca-certificates -f; 
ENV LD_LIBRARY_PATH=/opt/oracle/instantclient

#================
# Install Chrome and Firefox
#================
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
    apt-get -yqq update && \
    apt-get -yqq install google-chrome-stable && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get -qy --no-install-recommends install \
     $(apt-cache depends firefox | grep Depends | sed "s/.*ends:\ //" | tr '\n' ' ') \
  && rm -rf /var/lib/apt/lists/* \
  && cd /tmp \
  && wget --no-check-certificate -O firefox-esr.tar.bz2 \
    'https://download.mozilla.org/?product=firefox-esr-latest&os=linux64&lang=en-US' \
  && tar -xjf firefox-esr.tar.bz2 -C /opt/ \
  && ln -s /opt/firefox/firefox /usr/bin/firefox \
  && rm -f /tmp/firefox-esr.tar.bz2

#===========================
# Configure Virtual Display
#===========================
RUN set -e && \
    echo "Starting X virtual framebuffer (Xvfb) in background..." && \
    Xvfb -ac :99 -screen 0 1280x1024x16 > /dev/null 2>&1 & 
RUN export DISPLAY=:99 && \
    exec "$@"

#=============================================
# Allow Special Characters in Python Programs
#=============================================
RUN export PYTHONIOENCODING=utf8 && \
    echo "export PYTHONIOENCODING=utf8" >> ~/.bashrc

#=====================
# Set up SeleniumBase
#=====================
COPY requirements.txt /SeleniumBase/requirements.txt
RUN find /SeleniumBase -name '*.pyc' -delete && \
    find /SeleniumBase -name __pycache__ -delete && \
    pip3 install --upgrade pip && \
    pip3 install --upgrade setuptools && \
    pip3 install --upgrade setuptools-scm && \
    pip3 install wheel && \
    cd /SeleniumBase && ls && pip3 install -r requirements.txt --upgrade

#==========================================
# Download names and webdriverwrapper
#==========================================
#RUN pip3 install names webdriverwrapper==2.8.0

#==========================================
# Download WebDrivers
#==========================================
RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-linux32.tar.gz && \
    tar -xvzf geckodriver-v0.29.1-linux32.tar.gz && \
    chmod +x geckodriver && \
    mv geckodriver /usr/local/bin/ && \
    wget https://chromedriver.storage.googleapis.com/2.44/chromedriver_linux64.zip && \
    unzip chromedriver_linux64.zip && \
    mv chromedriver /usr/local/bin/ && \
    chmod +x /usr/local/bin/chromedriver

#================
# Install ChromeDriver
#================
RUN sbase install chromedriver latest

#==========================================
# Create entrypoint and grab example tests
#==========================================
CMD ["/bin/bash"]

Yes I tried the command line --maximize-window option as well. It didn't maximize my application.

However, my application is not being maximized. Here is a screenshot. As you can see, some of the buttons are one below the other and in the web table 'Matching Vehicles' I am expecting to see a link towards my right corner but it is not showing in view as the screen is not maximized. That's where my script is failing.

image

mdmintz commented 2 years ago

Have you tried using the latest version of SeleniumBase? (2.4.14) Your version is a year old and is missing important updates.

mdmintz commented 2 years ago

And have you tried using self.set_window_size(width, height) in your test with large dimensions?

mdmintz commented 2 years ago

Three different ways to solve your issue:

  1. Upgrade to version 2.4.14 (your version was 1.63.9)
  2. self.set_window_size(width, height) with large dimensions.
  3. Use a self.js_click(selector) instead of a regular self.click()
mdmintz commented 2 years ago

Closing this again, as one of the above 3 things should have resolved your issue.