You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker环境下Python+Selenium加载网页并点击按钮故障求助

Docker环境下Python+Selenium+Flask运行异常排查求助

在Docker环境中使用Python结合Selenium加载网页并点击按钮时,频繁遭遇各类异常:Selenium报错、页面无法加载、会话创建失败(SessionNotCreatedException)、选项配置错误、加载超时或完全无法运行。

已尝试的解决方案

  • 使用selenium/standalone-chrome作为Docker基础镜像,但无法通过pip安装Flask,提示“controlled externally”;
  • 尝试外部加载驱动,触发SessionNotCreatedException;
  • 尝试内部加载驱动,提示找不到ChromeDriver,安装过程无报错但卡住;
  • 脱离Flask以独立方式运行时一切正常,但封装进Docker后问题频发,且Selenium文档较为陈旧,无法提供有效帮助。

相关配置文件

Dockerfile

FROM python:3.11-slim-buster

USER root

# Create a non-root user
RUN useradd -ms /bin/bash appuser
WORKDIR /app
RUN chown appuser:appuser /app

USER appuser

COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy   
COPY src .

# Expose the application port (e.g., 5000)
EXPOSE 5000

# Define the command to run the application
CMD ["python3", "app.py"]

Docker-compose.yml

version: '3.8'

services:
  chrome:
    image: selenium/node-chrome:3.14.0-gallium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub
  hub:
    image: selenium/hub:3.14.0-gallium
    ports:
      - "4444:4444"

  web:
    build: .
    depends_on:
      - hub
    volumes:
      - ./src:/app
    ports:
      - "5000:5000"

app.py

from flask import Flask, render_template, request
import requests
import re
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import urllib.parse
from selenium.webdriver.chrome.options import Options
def download_page(url):
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.page_load_strategy = 'normal'
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--lang=en')
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--allow-running-insecure-content')
    chrome_options.add_argument('--disable-notifications')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-browser-side-navigation')
    chrome_options.add_argument('--mute-audio')
    chrome_options.add_argument('--force-device-scale-factor=1')
    chrome_options.add_argument('window-size=1080x760')
    driver = webdriver.Remote('http://hub:4444/wd/hub')

    driver.get(url)
    //Process page or click buttons

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/process', methods=['POST'])
def process():
    url = request.form['url'] 

    download_page(url)
    return "URL processing complete!"

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)

index.html

<!DOCTYPE html>
<html>
<head>
    <title>URL Processor</title>
</head>
<body>
    <h1>Enter a URL to process:</h1>
    <form method="POST" action="/process">
        <input type="text" name="url" placeholder="Enter URL here">
        <button type="submit">Process URL</button>
    </form>
</body>
</html>

内容的提问来源于stack exchange,提问作者Steven Venham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 08:52:37