linux上使用chrome自動化測試(無界面)

selenium自動化測試主要是用於有圖形界面的系統上,對於無圖形界面的狀況能夠經過如下方法來實現web

服務器信息chrome

[root@spider01 ~]# hostnamectl
Static hostname: spider01
Icon name: computer-vm
Chassis: vm
Machine ID: 1c4029c4e7fd42498e25bb75101f85b6
Boot ID: f5a67454b94b454fae3d75ef1ccab69f
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-514.6.2.el7.x86_64
Architecture: x86-64 centos

安裝Chromeium: bash

## 安裝yum源
[root@spider01 ~]# sudo yum install -y epel-release
## 安裝Chrome
[root@spider01 ~]# yum install -y chromium 服務器

ChromeDriver驅動 能夠經過https://sites.google.com/a/chromium.org/chromedriver/downloads進行下載,下載完成後放在/usr/bin/目錄下: tcp

完成結果以下: ide

[root@spider01 ~]# ll /usr/bin/ | grep chrom
-rwxrwxrwx. 1 root root 7500280 11月 29 17:32 chromedriver
lrwxrwxrwx. 1 root root 47 11月 30 09:35 chromium-browser -> /usr/lib64/chromium-browser/chromium-browser.sh 測試

安裝XVFB: google

[root@spider01 ~]# yum install Xvfb -y
[root@spider01 ~]# yum install xorg-x11-fonts* -y
新建在/usr/bin/ 一個名叫 xvfb-chromium 的文件寫入如下內容: 3d


[root@spider01 ~]# cat /usr/bin/xvfb-chromium
#!/bin/bash

_kill_procs() {
kill -TERM $chromium
wait $chromium
kill -TERM $xvfb
}

# Setup a trap to catch SIGTERM and relay it to child processes
trap _kill_procs SIGTERM

XVFB_WHD=${XVFB_WHD:-1280x720x16}

# Start Xvfb
Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &
xvfb=$!

export DISPLAY=:99

chromium --no-sandbox --disable-gpu$@ &
chromium=$!

wait $chromium
wait $xvfb

更改軟鏈接

## 更改Chrome啓動的軟鏈接

[root@spider01 ~]# ln -s /usr/lib64/chromium-browser/chromium-browser.sh /usr/bin/chromium


[root@spider01 ~]# rm -rf /usr/bin/chromium-browser

[root@spider01 ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/chromium-browser

[root@spider01 ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/google-chrome

[root@spider01 ~]# ll /usr/bin/ | grep chrom*
-rwxrwxrwx. 1 root root 7500280 11月 29 17:32 chromedriver
lrwxrwxrwx. 1 root root 47 11月 30 09:47 chromium -> /usr/lib64/chromium-browser/chromium-browser.sh
lrwxrwxrwx. 1 root root 22 11月 30 09:48 chromium-browser -> /usr/bin/xvfb-chromium
-rwxr-xr-x. 1 root root 73848 12月 7 2016 chronyc
lrwxrwxrwx. 1 root root 22 11月 30 09:48 google-chrome -> /usr/bin/xvfb-chromium
-rwxrwxrwx. 1 root root 387 11月 29 18:16 xvfb-chromium

測試是否可以正常使用>>> from selenium import webdriver >>> driver = webdriver.Chrome() >>> driver.get("http://www.baidu.com") >>> driver.find_element_by_xpath("./*//input[@id='kw']").send_keys("測試測試") >>> driver.find_element_by_xpath("./*//input[@id='su']").click() >>> driver.page_source

相關文章
相關標籤/搜索