教你如何搭建web应用程序测试工具selenium

window

首先当然是安装chrome了,然后安装python,最后安装chromedriver,window平台文档甚多就不啰嗦了。
这里发现一个很有意思的现象,如果没有安装python,在powershell里面直接输入python就会跳转到应用商店点获取就能安装了
下载chromedriver http://npm.taobao.org/mirrors/chromedriver/80.0.3987.16/chromedriver_win32.zip 与chrome保持大版本一致即可
将chromedriver.exe复制到chrome安装目录(C:\Program Files (x86)\Google\Chrome\Application),将目录添加进PATH环境变量即可,当然也不一定非要复制到chrome安装目录,只要配置环境变量复制到那里都是一样的。
最后打开powershell下载selenium
pip install selenium

验证是否成功

在命令行模式执行如下命即可,当然写成脚本执行更好

# chromium
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()

# 设置无窗口模式,这个在Linux服务器上一定要设置不然总是会报浏览器崩溃的错,当然window就无所谓了
# 之前也找到了这种写法options.add_argument('--headless'),不过应该是最新的selenium修改了导致配置无效浪费很多时间,详情可以查看官方文档
options.add_argument('headless')
options.add_argument('disable-gpu')
options.add_argument('no-sandbox')
driver = webdriver.Chrome(options=options)
driver.get('http://baidu.com')
# print(driver.title)
print(driver.page_source)

# firefox
from selenium import webdriver
browser = webdriver.Firefox()//webdriver.Chrome()
browser.get('http://www.baidu.com/')
print(browser.title)

ubuntu


apt-get update -y && apt-get install -y chromium xvfb python3 python3-pip 
apt-get install -y unzip libgconf-2-4

# 在这里下载的chromedriver版本可能与chromium版本不一致可在淘宝源查看相应版本替换,版本一定要一致不然会报错,当然大版本一致即可,例如chromium为80,chromedriver也为80
# chromium -version可查看chrome版本
# 淘宝源地址:http://npm.taobao.org/mirrors/chromedriver/
wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
pip install selenium

dockerfile

附赠几个dockerfile

# chromium
FROM python:latest
MAINTAINER  <772026318@qq.com>
RUN apt-get update -y && apt-get install -y chromium xvfb
RUN apt-get install -y libgconf-2-4
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
RUN pip install selenium

# firefox
FROM python:latest
MAINTAINER <hpe_cd_zzh@163.com>
RUN apt-get update -y && apt-get install -y xvfb
RUN apt-get install -y libgconf-2-4 libappindicator3-1
RUN pip install selenium
COPY config /tmp/config
RUN tar -xvf /tmp/config/geckodriver-v0.26.0-linux64.tar.gz -C /usr/bin/ \
    && tar -xvf /tmp/config/firefox-77.0.1.tar.bz2 -C /opt/ \
    && ln -fs /opt/firefox/firefox /usr/bin/firefox
ENV DISPLAY=:10
ENTRYPOINT Xvfb :10 -ac

# chromium and firefox
FROM python:latest
MAINTAINER <hpe_cd_zzh@163.com>
RUN apt-get update -y && apt-get install -y xvfb chromium
RUN apt-get install -y libgconf-2-4
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
RUN pip install selenium
COPY config /tmp/config
RUN tar -xvf /tmp/config/geckodriver-v0.26.0-linux64.tar.gz -C /usr/bin/ \
    && tar -xvf /tmp/config/firefox-77.0.1.tar.bz2 -C /opt/ \
    && ln -fs /opt/firefox/firefox /usr/bin/firefox
ENV DISPLAY=:10
ENTRYPOINT Xvfb :10 -ac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值