这是我的 docker 文件部分,用于下载 chrome 和 chromedriver,我正在使用 xvfb 在虚拟显示器中录制屏幕。
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 代码部分:
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:
stop_screen_recording(ffmpeg_process)
# Quit the browser
# if "driver" in locals():
if driver:
driver.quit()
url = 'https://pythonbasics.org'
# ffmpeg docker path:
ffmpeg_path = "/usr/bin/ffmpeg"
# docker driver path:
driver_path = "/usr/bin/chromedriver"
# to run from docker
output_file = "test.mp4"
print('starting the script...')
record_webpage(url=url, output_file=output_file,
ffmpeg_path=ffmpeg_path, driver_path=driver_path)
我在 docker 运行期间遇到错误。错误:selenium.common.exceptions.SessionNotCreatedException:消息:会话未创建:Chrome 无法启动:正常退出。 (会话未创建:DevToolsActivePort 文件不存在)(从 chrome 位置 /usr/bin/google-chrome 启动的进程不再运行,因此 ChromeDriver 假设 Chrome 已崩溃。) Stacktrace:...... ......
我可以在 dockerfile 或 selenium 爬行代码中改进什么?
我添加了 chrome 选项: chrome_options.add_argument("--无沙箱") 它起作用了。