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.06k stars 946 forks source link

Stuck in certificate selection #2390

Closed Ramonrune closed 8 months ago

Ramonrune commented 8 months ago

Hi!

I'am using selenium base to bypass one site. It works really well on my PC and on EC2/Virtual box. I'am creating a docker container for it, but when i run the code inside a container, it stucks in certificate selection part.

Here is a simple code:

pkcs12_file='file.pfx'
password="password"
install_certificate(pkcs12_file, password)

args ="--lang=pt-BR,"    
args += ' --verbose,'    
args += f"--load-extension={os.path.join(os.getcwd(), 'captcha_extension')}"
chromium_arg = '--auto-select-certificate-for-urls={"pattern":"*","filter":{}}' + args
driver = Driver(
    uc=True,
    headless=True,
    browser="chrome",
    chromium_arg=chromium_arg
)

driver.get("")
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "sign-in")))
driver.find_element(By.CLASS_NAME, "sign-in").click()
driver.find_element(By.ID, "login").click() # Code stops here
print("Passed")

Here is my dockerfile:


FROM debian:bullseye-slim

# Install Chrome
RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}-1_amd64.deb \
  && apt-get install -y /tmp/chrome.deb \
  && rm /tmp/chrome.deb

# Update the package repository and install necessary packages
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip

RUN apt-get install libnss3-tools -y

RUN mkdir -p $HOME/.pki/nssdb

RUN mkdir -p  $HOME/etc/opt/chrome/policies/managed
COPY policies.json  $HOME/etc/opt/chrome/policies/managed/policies.json
RUN pip install requests

COPY entrypoint.sh /tmp/entrypoint.sh

COPY requirements.txt /tmp/

RUN pip install --requirement /tmp/requirements.txt
COPY main.py /main.py

ENTRYPOINT ["/bin/bash", "/tmp/entrypoint.sh"]

In the final part, there is a captcha too, which i use another solution to break (not on the above code). Notice that policies.json is the following: { "AutoSelectCertificateForUrls": ["{\"pattern\":\"*\",\"filter\":{}}"] }

Any ideas about what is could be? Code runs easily on my pc, but in container it is stucked.

mdmintz commented 8 months ago

I see several things that can be improved:


(Use existing args rather than passing to chromium_arg when you can.)


(There's a Stack Overflow post about the newer headless mode here: https://stackoverflow.com/a/73840130/7058266 - which is triggered by headless2 in SeleniumBase.)


(The Driver() manager doesn't have xvfb as a direct arg.)


You shouldn't have to use EC.presence_of_element_located or any of the WebDriverWait syntax at all.

Use the built-in click methods for clicking:

driver.click(selector)

driver.js_click(selector)

driver.uc_click(selector)

(And especially since you're using UC Mode, you'll may need to use one of those to click without getting stuck.)

Ramonrune commented 8 months ago

Thanks! I will test these approaches

Ramonrune commented 8 months ago

Hi again!

Did the changes, but still not working, here is the new code:


args += " --shm-size=2g,"
args += " --privileged,"
args +=  " --verbose,"

chromium_arg = '--auto-select-certificate-for-urls={"pattern":"*","filter":{}}' + args
print(chromium_arg)

display = Display(visible=0, size=(1440, 1880))
display.start()

with SB(
    uc=True,
    headless2=True,
    browser="chrome",
    chromium_arg=chromium_arg,
    extension_dir=os.path.join(os.getcwd(), 'nocaptchaai_chrome'),
    xvfb=display
) as driver:

    driver.get("https://login.esocial.gov.br/login.aspx")

    driver.click(".sign-in")

    driver.click("#login-certificate") # Stucks here
Ramonrune commented 8 months ago

8cXZH

It stops in this screen. But when running on my local computer, it runs with no problem.

Here is my container:


FROM debian:bullseye-slim

ENV USER=root

ENV DISPLAY :1

RUN  apt-get update && apt-get install -y --no-install-recommends \
    libterm-readkey-perl ca-certificates wget expect iproute2 iputils-ping curl procps libnm0 

RUN apt-get update && apt-get install -y \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libatspi2.0-0 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libdrm2 \
    libexpat1 \
    libgbm1 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libpango-1.0-0 \
    libu2f-udev \
    libvulkan1 \
    libx11-6 \
    libxcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxkbcommon0 \
    libxrandr2 \
    xdg-utils \
    vim

# Install Chrome
RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}-1_amd64.deb \
  && apt-get install -y /tmp/chrome.deb \
  && rm /tmp/chrome.deb

# Update the package repository and install necessary packages
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip

RUN apt-get install libnss3-tools -y
RUN apt-get install xvfb -y

ENV DISPLAY=:99
ENV DBUS_SESSION_BUS_ADDRESS=/dev/null

RUN mkdir -p $HOME/.pki/nssdb

RUN mkdir -p  $HOME/etc/opt/chrome/policies/managed
COPY policies.json  $HOME/etc/opt/chrome/policies/managed/policies.json

RUN mkdir -p  /etc/opt/chrome/policies/managed
COPY policies.json  /etc/opt/chrome/policies/managed/policies.json

RUN pip install requests

COPY entrypoint.sh /tmp/entrypoint.sh
COPY expressvpn-activate.sh /tmp/expressvpn-activate.sh

COPY drtv.pfx /drtv.pfx

RUN pip install --requirement /tmp/requirements.txt
COPY main.py /main.py

CMD xvfb-run --server-args="-screen 0 1024x768x24" python3 main.py

@mdmintz Any ideas?

mdmintz commented 8 months ago

Looks more like a Dockerfile config issue than a SeleniumBase one since it's working in your other environment. I don't have any more suggestions to offer other than the ones I already provided.

Ramonrune commented 8 months ago

Hi @mdmintz , i found the problem!

If anyone has this problem, i solved it in this way:

On the last line of docker:
RUN apt-get install -y libnss3-tools wget xvfb x11vnc fluxbox
These two lines will be used for remote connection and debugging. 
RUN mkdir ~/.vnc
RUN x11vnc -storepasswd 123456 ~/.vnc/passwd  # Replace 123456 with your desired VNC password
Here is the trick
CMD ["sh", "-c", "Xvfb :0 -screen 0 1024x768x16 & fluxbox & x11vnc -forever -usepw -create & python main.py"]

Xvfb: Display server
fluxbox: Windows manager
x11vnc: remote access 

On main,py:

from sbvirtualdisplay import Display
display = Display(visible=0, size=(1440, 1880))
display.start()

with SB(uc=True,
                headless=False,
                browser="chrome",
                chromium_arg=chromium_arg,
                xvfb=display) as driver:

Best regards,