This is my docker file section to download chrome and chromedriver and I’m using xvfb to record the screen in a virtual display.
RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o
/usr/share/keyrings/google-linux-signing-key.gpg
&& sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-linux-signing-
key.gpg] http://dl.google.com/linux/chrome/deb/ stable main" >>
/etc/apt/sources.list.d/google-chrome.list'
&& apt-get update
&& apt-get install -y google-chrome-stable
&& rm -rf /var/lib/apt/lists/*
&& apt-get clean
RUN wget -q -O /tmp/chromedriver.zip storage.googleapis.com/chrome-for-
testing-public/126.0.6478.126/…
&& unzip -o /tmp/chromedriver.zip -d
/tmp/ >> /build_logs/chromedriver_install.log
&& ls -la
/tmp/chromedriver-linux64
&& mv /tmp/chromedriver-linux64/chromedriver
/usr/bin/chromedriver
&& chmod +x /usr/bin/chromedriver
&& rm /tmp/chromedriver.zip
Selenium code section:
driver = None
try:
# Configure Chrome options for headless mode
chrome_options = Options()
# chrome_options.add_argument("--headless") # Ensure headless mode is enabled
chrome_options.add_argument("--window-size=1920,1080") # Set window size
chrome_options.add_argument("--disable-gpu") # Disable GPU acceleration
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-notifications")
# Specify the path to your ChromeDriver
# driver_path = 'D:/chromedriver-win64/chromedriver.exe'
# Ensure the correct path to chromedriver
chrome_service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
# Navigate to the webpage
driver.get(url)
# Allow time for the page to load and for recording to capture
time.sleep(10) # Adjust as needed
print(driver.title)
finally:
# Quit the browser
# if "driver" in locals():
if driver:
driver.quit()
I’m getting errors during docker run. Error: selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally. (session not created: DevToolsActivePort file doesn’t exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace:…………
What can I improve in the dockerfile or the selenium crawl code?