仅用于交流和学习,禁止利用本资源从事任何违反本国(地区)法律法规的活动,一切遵守《网络安全法》
Selenium介绍
- 行业简称 无头浏览器
- 主要用于自动化测试
- 也用于模拟用户操作进行爬虫
- 框架底层使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动按照脚本代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样,从终端用户的角度测试应用程序。
- 使浏览器兼容性测试自动化成为可能,尽管在不同的浏览器上依然有细微的差别。
- 使用简单,可使用Java,Python等多种语言编写用例脚本。
- 安装 java环境
[root@ar414.com ~] yum -y install java
2.安装 chrome
#使用下面的命令,在root用户下就可以安装最新的 Google Chrome
[root@ar414.com ~] yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
3.安装 selenium
- 在selenium官网找到最新的版本,下载 selenium-server-standalone-X.XX.X.jar文件
4.安装 chromedriver
- 在chromedriver下载对应Chrom版本的ChromeDriver压缩包,解压得到chromedriver文件
chromedriver_linux64.zip
5.将下载的文件解压,放在如下位置
[root@ar414.com ~] unzip ./chromedriver_linux64.zip
[root@ar414.com ~] mv ./chromedriver /usr/bin/chromedriver
6.给予执行权限
[root@ar414.com ~] vim /usr/bin/xvfb-chrome
7.安装 XVFB
[root@ar414.com ~] yum install Xvfb -y
[root@ar414.com ~] yum install xorg-x11-fonts* -y
8.新建在/usr/bin/ 一个名叫 xvfb-chrom 的文件写入以下内容
#!/bin/bash
_kill_procs() {
kill -TERM $chrome
wait $chrome
kill -TERM $xvfb
}
Setup a trap to catch SIGTERM and relay it to child processes
rap _kill_procs SIGTERM
VFB_WHD=${XVFB_WHD:-1280x720x16}
Start Xvfb
vfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &
vfb=$!
xport DISPLAY=:99
chrome --no-sandbox --disable-gpu$@ &
chrome=$!
wait $chrome
wait $xvfb
9.添加执行权限
chmod +x /usr/bin/xvfb-chrome
10.查看当前映射关系
[root@ar414.com ~] ll /usr/bin/ | grep chrom
-rwxr-xr-x 1 root root 7874704 Mar 20 14:55 chromedriver
lrwxrwxrwx 1 root root 31 Mar 20 00:24 google-chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx 1 root root 32 Mar 20 14:30 google-chrome-stable -> /opt/google/chrome/google-chrome
11.更改Chrome启动的软连接
[root@ar414.com ~] ln -s /etc/alternatives/google-chrome /usr/bin/chrome
[root@ar414.com ~] rm -rf /usr/bin/google-chrome
[root@ar414.com ~] ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome
12.查看修改后的映射关系
[root@ar414.com ~] ll /usr/bin/ | grep chrom
-rwxr-xr-x 1 root root 7874704 Mar 20 14:55 chromedriver
lrwxrwxrwx 1 root root 31 Mar 20 00:24 chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx 1 root root 22 Mar 20 00:11 google-chrome -> /usr/bin/xvfb-chromium
lrwxrwxrwx 1 root root 32 Mar 20 14:30 google-chrome-stable -> /opt/google/chrome/google-chrome
-rwxr-xr-x 1 root root 432 Mar 20 00:09 xvfb-chrome
13.后台运行selenium服务
#Tip:一般我线上环境都会用supervisor守护进程来保证服务一直处于运行状态,在遇到程序异常、报错等情况可以立刻重启,继续提供服务,有时间会写一个supervisor的实战文章
[root@ar414.com ~] nohup java -jar selenium-server-standalone-3.141.59.jar &
网友评论