Docker selenium自动化 - Python调用容器实例跑自动化查天气实例演示,docker selenium自动化环境部署过程
Docker selenium自動化 - 環境部署與 Python 自動化運行實戰演示
- 第一章:docker selenium 環境部署
- ① 下載 selenium 鏡像
- ② 容器 selenium 實例的創建與運行
- ③ 瀏覽器訪問 selenium Grid
- ④ docker 命令大全
- 第二章:Python 調用 docker selenium 執行自動化實例演示
- ① 源碼展示
- ② 運行效果
[系列文章篇]
篇章一:Docker selenium 自動化 - windows 版 docker 的安裝與運行環境檢測
[問題處理篇]
篇章一:Docker selenium 自動化 - 修改 /dev/shm 路徑大小實例演示,“session deleted because of page crash” 問題解決
第一章:docker selenium 環境部署
① 下載 selenium 鏡像
首先拉取 selenium/hub 鏡像。
執行命令:docker pull selenium/hub
然后拉取 selenium/node-chrome 鏡像。
執行命令:docker pull selenium/node-chrome
docker images 查看容器鏡像。
② 容器 selenium 實例的創建與運行
創建容器實例:
docker run -d -P --name=hub selenium/hub
docker run -d -P --link=hub:hub selenium/node-chrome
查看當前運行的容器實例:
docker ps
查看所有的容器實例:
docker ps -a
可以通過 docker start/stop 實例名 來啟動停用容器實例。
一般服務器重啟后,就需要來啟動容器了。
③ 瀏覽器訪問 selenium Grid
docker ps 可以查看生成的端口,通過本地 ip + 端口號 即可訪問 selenium Grid 控制臺。
可以看到瀏覽器版本相關信息。
可以看到相關配置信息。
④ docker 命令大全
docker 相關命令
docker --helpUsage: docker [OPTIONS] COMMANDA self-sufficient runtime for containersOptions:--config string Location of client config files (default"C:\\Users\\Administrator\\.docker")-c, --context string Name of the context to use to connect to thedaemon (overrides DOCKER_HOST env var anddefault context set with "docker context use")-D, --debug Enable debug mode-H, --host list Daemon socket(s) to connect to-l, --log-level string Set the logging level("debug"|"info"|"warn"|"error"|"fatal")(default "info")--tls Use TLS; implied by --tlsverify--tlscacert string Trust certs signed only by this CA (default"C:\\Users\\Administrator\\.docker\\ca.pem")--tlscert string Path to TLS certificate file (default"C:\\Users\\Administrator\\.docker\\cert.pem")--tlskey string Path to TLS key file (default"C:\\Users\\Administrator\\.docker\\key.pem")--tlsverify Use TLS and verify the remote-v, --version Print version information and quitManagement Commands:builder Manage buildsbuildx* Build with BuildKit (Docker Inc., v0.6.3)compose* Docker Compose (Docker Inc., v2.0.0)config Manage Docker configscontainer Manage containerscontext Manage contextsimage Manage imagesmanifest Manage Docker image manifests and manifest listsnetwork Manage networksnode Manage Swarm nodesplugin Manage pluginsscan* Docker Scan (Docker Inc., v0.8.0)secret Manage Docker secretsservice Manage servicesstack Manage Docker stacksswarm Manage Swarmsystem Manage Dockertrust Manage trust on Docker imagesvolume Manage volumesCommands:attach Attach local standard input, output, and error streams to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogin Log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerps List containerspull Pull an image or a repository from a registrypush Push an image or a repository to a registryrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagesrun Run a command in a new containersave Save one or more images to a tar archive (streamed to STDOUT by default)search Search the Docker Hub for imagesstart Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containersversion Show the Docker version informationwait Block until one or more containers stop, then print their exit codesRun 'docker COMMAND --help' for more information on a command.To get more help with docker, check out our guides at https://docs.docker.com/go/guides/docker run 相關命令
docker run --helpUsage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]Run a command in a new containerOptions:--add-host list Add a custom host-to-IP mapping(host:ip)-a, --attach list Attach to STDIN, STDOUT or STDERR--blkio-weight uint16 Block IO (relative weight),between 10 and 1000, or 0 todisable (default 0)--blkio-weight-device list Block IO weight (relative deviceweight) (default [])--cap-add list Add Linux capabilities--cap-drop list Drop Linux capabilities--cgroup-parent string Optional parent cgroup for thecontainer--cgroupns string Cgroup namespace to use(host|private)'host': Run the container inthe Docker host's cgroup namespace'private': Run the container inits own private cgroup namespace'': Use the cgroupnamespace as configured by thedefault-cgroupns-modeoption on the daemon (default)--cidfile string Write the container ID to the file--cpu-period int Limit CPU CFS (Completely FairScheduler) period--cpu-quota int Limit CPU CFS (Completely FairScheduler) quota--cpu-rt-period int Limit CPU real-time period inmicroseconds--cpu-rt-runtime int Limit CPU real-time runtime inmicroseconds-c, --cpu-shares int CPU shares (relative weight)--cpus decimal Number of CPUs--cpuset-cpus string CPUs in which to allow execution(0-3, 0,1)--cpuset-mems string MEMs in which to allow execution(0-3, 0,1)-d, --detach Run container in background andprint container ID--detach-keys string Override the key sequence fordetaching a container--device list Add a host device to the container--device-cgroup-rule list Add a rule to the cgroup alloweddevices list--device-read-bps list Limit read rate (bytes per second)from a device (default [])--device-read-iops list Limit read rate (IO per second)from a device (default [])--device-write-bps list Limit write rate (bytes persecond) to a device (default [])--device-write-iops list Limit write rate (IO per second)to a device (default [])--disable-content-trust Skip image verification (default true)--dns list Set custom DNS servers--dns-option list Set DNS options--dns-search list Set custom DNS search domains--domainname string Container NIS domain name--entrypoint string Overwrite the default ENTRYPOINTof the image-e, --env list Set environment variables--env-file list Read in a file of environment variables--expose list Expose a port or a range of ports--gpus gpu-request GPU devices to add to thecontainer ('all' to pass all GPUs)--group-add list Add additional groups to join--health-cmd string Command to run to check health--health-interval duration Time between running the check(ms|s|m|h) (default 0s)--health-retries int Consecutive failures needed toreport unhealthy--health-start-period duration Start period for the container toinitialize before startinghealth-retries countdown(ms|s|m|h) (default 0s)--health-timeout duration Maximum time to allow one check torun (ms|s|m|h) (default 0s)--help Print usage-h, --hostname string Container host name--init Run an init inside the containerthat forwards signals and reapsprocesses-i, --interactive Keep STDIN open even if not attached--ip string IPv4 address (e.g., 172.30.100.104)--ip6 string IPv6 address (e.g., 2001:db8::33)--ipc string IPC mode to use--isolation string Container isolation technology--kernel-memory bytes Kernel memory limit-l, --label list Set meta data on a container--label-file list Read in a line delimited file of labels--link list Add link to another container--link-local-ip list Container IPv4/IPv6 link-localaddresses--log-driver string Logging driver for the container--log-opt list Log driver options--mac-address string Container MAC address (e.g.,92:d0:c6:0a:29:33)-m, --memory bytes Memory limit--memory-reservation bytes Memory soft limit--memory-swap bytes Swap limit equal to memory plusswap: '-1' to enable unlimited swap--memory-swappiness int Tune container memory swappiness(0 to 100) (default -1)--mount mount Attach a filesystem mount to thecontainer--name string Assign a name to the container--network network Connect a container to a network--network-alias list Add network-scoped alias for thecontainer--no-healthcheck Disable any container-specifiedHEALTHCHECK--oom-kill-disable Disable OOM Killer--oom-score-adj int Tune host's OOM preferences (-1000to 1000)--pid string PID namespace to use--pids-limit int Tune container pids limit (set -1for unlimited)--platform string Set platform if server ismulti-platform capable--privileged Give extended privileges to thiscontainer-p, --publish list Publish a container's port(s) tothe host-P, --publish-all Publish all exposed ports torandom ports--pull string Pull image before running("always"|"missing"|"never")(default "missing")--read-only Mount the container's rootfilesystem as read only--restart string Restart policy to apply when acontainer exits (default "no")--rm Automatically remove the containerwhen it exits--runtime string Runtime to use for this container--security-opt list Security Options--shm-size bytes Size of /dev/shm--sig-proxy Proxy received signals to theprocess (default true)--stop-signal string Signal to stop a container(default "15")--stop-timeout int Timeout (in seconds) to stop acontainer--storage-opt list Storage driver options for thecontainer--sysctl map Sysctl options (default map[])--tmpfs list Mount a tmpfs directory-t, --tty Allocate a pseudo-TTY--ulimit ulimit Ulimit options (default [])-u, --user string Username or UID (format:<name|uid>[:<group|gid>])--userns string User namespace to use--uts string UTS namespace to use-v, --volume list Bind mount a volume--volume-driver string Optional volume driver for thecontainer--volumes-from list Mount volumes from the specifiedcontainer(s)-w, --workdir string Working directory inside the container第二章:Python 調用 docker selenium 執行自動化實例演示
① 源碼展示
# -*- coding: UTF8 -*- # 2021.11.16 # 小藍棗 # docker selenium 自動化from selenium import webdriver from time import sleepdriver = webdriver.Remote(command_executor='http://127.0.0.1:55006/wd/hub',desired_capabilities={'browserName': 'chrome'} )try:# 登錄中國氣象網查看北京天氣driver.get('http://www.weather.com.cn/weather1d/101010100.shtml')sleep(3)# 讀取天氣信息bj_temperature = driver.find_element_by_xpath('//*[@class="sk mySkyNull"]//*[@class="tem"]/*').textbj_wind_direction = driver.find_element_by_xpath('//*[@class="sk mySkyNull"]//*[@class="zs w"]/span').textbj_wind_class = driver.find_element_by_xpath('//*[@class="sk mySkyNull"]//*[@class="zs w"]/em').textbj_air_quality = driver.find_element_by_xpath('//*[@class="sk mySkyNull"]//*[@class="zs pol"]//a').textbj_weather = '''城市:北京當前溫度:%s風向:%s風力:%s空氣質量:%s'''%(bj_temperature,bj_wind_direction,bj_wind_class,bj_air_quality)# 打印抓取的天氣信息print(bj_weather)# 保存截圖driver.get_screenshot_as_file("docker_selenium_run_001.png")# 保證出錯后進程正常釋放 finally:driver.quit()② 運行效果
抓取的天氣信息如下:
同時可以看到保存的截圖。
喜歡的點個贊?吧!
總結
以上是生活随笔為你收集整理的Docker selenium自动化 - Python调用容器实例跑自动化查天气实例演示,docker selenium自动化环境部署过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker selenium自动化 -
- 下一篇: Docker selenium自动化 -