Docker之搭建Ubuntu+Google-Chrome+Selenium

Selenium是浏览器功能自动化测试工具,在终端界面下也能运行,所以可作为爬虫的 js 引擎。

Dockerfile:

FROM ubuntu:16.04
MAINTAINER tuweifeg "907391489@qq.com"

RUN 	apt update; \
	apt install -y  bzip2 \
			unzip \
			vim \
			wget \
			libxss1 \
			libappindicator1 \
			xvfb \
			libindicator7
#			gdebi


RUN	mkdir -p /home/ubuntu/project; \
	mkdir -p /home/ubuntu/soft; \
	cd /home/ubuntu/soft; \
	wget https://www.slimjet.com/chrome/download-chrome.php?file=files%2F75.0.3770.80%2Fgoogle-chrome-stable_current_amd64.deb; \
	apt install -y ./*google-chrome*.deb; \
	rm *google-chrome*.deb; \
#	gdebi *google-chrome*.deb; \

	wget https://npm.taobao.org/mirrors/chromedriver/75.0.3770.90/chromedriver_linux64.zip; \
	unzip chromedriver_linux64.zip; \
	rm chromedriver_linux64.zip; \
	
	wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh; \
	sh Anaconda3-5.0.1-Linux-x86_64.sh -b; \
	rm Anaconda3-5.0.1-Linux-x86_64.sh; \
	echo 'export PATH=/
### 回答1: 在 GitLab CI 中使用 Docker 时,可以在 .gitlab-ci.yml 文件中使用 chrome。这需要在 Dockerfile 中安装 chrome 浏览器chromedriver,并在 .gitlab-ci.yml 文件中配置启动 chromechromedriver。具体步骤如下: 1. 在 Dockerfile 中安装 chrome 浏览器chromedriver,可以使用以下命令: ``` RUN apt-get update && apt-get install -y google-chrome-stable RUN wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip RUN unzip chromedriver_linux64.zip -d /usr/bin/ ``` 2. 在 .gitlab-ci.yml 文件中配置启动 chromechromedriver。 ``` services: - chrome:latest before_script: - export CHROME_BIN=/usr/bin/google-chrome - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start ``` 3. 在你的测试代码中配置使用chrome ``` from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.binary_location = os.environ.get("CHROME_BIN") chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--remote-debugging-port=9222") chrome_options.add_argument("start-maximized") chrome_options.add_argument("disable-infobars") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-browser-side-navigation") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--remote-debugging-port=9222") chrome_options.add_argument("--disable-setuid-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-infobars") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--disable-gpu-sandbox") chrome_options.add_argument("--disable- ### 回答2: 在gitlab-ci的docker环境中使用chrome可以通过以下步骤实现: 1. 首先,在.gitlab-ci.yml文件中定义一个新的job,这个job将用来运行chrome。 ``` yml chrome_test: image: selenium/standalone-chrome script: - # 添加运行chrome的命令 ``` 2. 在上述job中使用selenium/standalone-chrome镜像作为基础镜像。这个镜像已经预装了chrome浏览器。 3. 在script脚本中可以添加需要运行的chrome命令。 ```yml chrome_test: image: selenium/standalone-chrome script: - google-chrome-stable --version - # 添加其他需要运行的chrome命令 ``` 上述脚本中的google-chrome-stable --version是一个示例命令,用于显示chrome浏览器的版本信息。你可以根据需要添加其他需要运行的chrome命令。 4. 确保在.gitlab-ci.yml文件中定义的job在pipeline中被执行。 通过以上步骤,就可以在gitlab-ci的docker环境中使用chrome浏览器了。你可以在script脚本中运行各种需要使用chrome的测试或其他命令。注意,由于使用的是selenium/standalone-chrome镜像,因此脚本中的命令需要以google-chrome-stable开头。 ### 回答3: 要在GitLab CI中的Docker容器中使用Chrome,需要按照以下步骤进行配置: 1. 在.gitlab-ci.yml文件中的job中定义Docker镜像,选择一个已包含Chrome的镜像,例如google/chrome。 示例: ``` image: google/chrome jobs: test: script: - chrome --version # 做一些其他的测试操作 ``` 2. 在Docker镜像中安装Chrome,可以通过在Dockerfile中使用apt-get命令或者直接使用chromeDocker镜像。 Dockerfile示例: ```Dockerfile FROM ubuntu # 安装Chrome RUN apt-get update && apt-get -y install wget gnupg RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list RUN apt-get -y update RUN apt-get install -y google-chrome-stable # 可以进行其他的Docker镜像配置操作 CMD ["/bin/bash"] ``` 3. 在job的脚本中使用Chrome命令,运行相关的测试或操作。 示例: ```shell script: - google-chrome-stable --version - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://your-website.com # 其他的测试或操作命令 ``` 通过以上步骤,你就可以在GitLab CI的Docker容器中成功使用Chrome进行相关的测试或操作了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值