selenium

$ docker pull selenium/standalone-chrome:latest
$ docker images
REPOSITORY                   TAG       IMAGE ID       CREATED         SIZE
selenium/standalone-chrome   latest    7724abd7872b   3 days ago      1.46GB

$ docker run --name selenium -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:latest

$ docker run --name selenium -d -p 4444:4444 -p 7900:7900 --net host --shm-size="2g" selenium/standalone-chrome:latest

$ docker run --name selenium -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:120.0

1
2
3
4
5
6
7
8
9
10
11
$ docker run --name selenium --rm -it -p 4444:4444 -p 5900:5900 -p 7900:7900 --shm-size 2g seleniarm/standalone-chromium:117.0

$ docker run --name selenium -d -p 4444:4444 -p 5900:5900 -p 7900:7900 --shm-size 2g seleniarm/standalone-chromium:117.0
1
2
3

进入python容器环境,执行如下:

$ pip install selenium
1

from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote( command_executor="http://127.0.0.1:4444/wd/hub", desired_capabilities=DesiredCapabilities.CHROME )

driver.get("http://www.baidu.com") print(driver.title) driver.close()


#!/usr/bin/env python

from selenium import webdriver from selenium.webdriver.common.by import By import time

selenium_grid_url = 'http://127.0.0.1:4444'

selenium

option = webdriver.ChromeOptions() option.set_capability("browserName", "chrome") option.set_capability("browserVersion", "120.0") option.set_capability("platformName", "linux") wd = webdriver.Remote(command_executor=selenium_grid_url, options=option)

wd.implicitly_wait(10)

调用WebDriver 对象的get方法 可以让浏览器打开指定网址

wd.get('https://www.baidu.com/')

print(wd.title) # 打印网页标题

wd.quit()