官网说明书:https://github.com/SeleniumHQ/docker-selenium
执行:
docker run -d -p 4444:4444 selenium/standalone-chrome:98.0
Python代码:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
# 创建一个参数对象,用来控制chrome是否以*面模式打开
ch_op = Options()
# 设置谷歌浏览器的页面无可视化,如果需要可视化请注释这两行代码
ch_op.add_argument('--headless')
ch_op.add_argument('--disable-gpu')
# ch_op.add_argument('disable-dev-shm-usage')
driver = webdriver.Remote(
command_executor="http://0.0.0.0:4444/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME,
options=ch_op
)
driver.get("http://www.baidu.com")
print(driver.page_source)
driver.quit()