Skip to content

Instantly share code, notes, and snippets.

@stanislavhordiyenko
Last active May 29, 2023 06:02
Show Gist options
  • Save stanislavhordiyenko/b0a256c5dcdf246cea865239be73f86a to your computer and use it in GitHub Desktop.
Save stanislavhordiyenko/b0a256c5dcdf246cea865239be73f86a to your computer and use it in GitHub Desktop.

Last revision: 29th May 2023.

How to build and run Docker image locally:

# uncomment the platform you are running on
# platform=linux/amd64
platform=linux/arm64

docker buildx build --platform=$platform -t selenium-firefox-headless:latest .

# if you are running Docker Desktop, make sure that the current path ($(pwd)) 
# is added to the list of trusted directories. Otherwise, you won't be
# able to mount it in the running container:
#   Preferences... > Resources > File sharing
docker run --rm \
       --platform=$platform \
       --volume $(pwd):/tests \
       -it selenium-firefox-headless:latest /bin/bash
       
# run the test below
python main.py

Don't hesitate to file a PR with improvement(s).

FROM python:3.8-slim
WORKDIR /tests
RUN apt update && \
apt install -y --no-install-recommends firefox-esr=102.10.0esr-1~deb11u1 && \
apt install -y curl && \
apt clean && \
curl -L https://github.com/mozilla/geckodriver/releases/download/v0.33.0/geckodriver-v0.33.0-linux64.tar.gz -o geckodriver.tar.gz && \
tar xvf geckodriver.tar.gz && \
rm -rf geckodriver.tar.gz && \
mv geckodriver /usr/local/bin/geckodriver && \
chmod +x /usr/local/bin/geckodriver
RUN pip install -U pip && \
pip install --no-cache-dir selenium==4.8.3
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.get("https://www.google.com")
print(driver.title)
search_box = driver.find_element("name", "q")
search_box.send_keys("Selenium Webdriver")
search_box.send_keys(Keys.RETURN)
time.sleep(5)
print(driver.title)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment