1 install java
# yum search java-11-openjdk
# yum install java-11-openjdk.x86_64
2 install chrome for linux
# yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
2 view chrome version, # yum list |grep chrome
3 install chromedriver for linux
down chromedriver for linux
? #?chmod 755 chromedriver_linux64.zip
? #?unzip chromedriver_linux64.zip
? #?chmod 777 chromedriver
? #?cp chromedriver /usr/bin/chromedriver
4 install python3.7.7
# yum install epel-release -y
# cd /opt
# wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tar.xz
# yum install -y python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make
# tar -xvf Python-3.7.7.tar.xz
# cd Python-3.7.7
# ./configure --with-ssl --prefix=/usr/local/python3
# make && make install
# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
4 生成python虛擬環(huán)境
# python3 -m venv venv
進入虛擬環(huán)境
# source venv/bin/activate
查看安裝包
# pip freeze
5 安裝selenium
# pip3 install selenium==3.141.0 -i https://pypi.douban.com/simple
# pip3 install urllib3==1.26.2? 重裝低版本
# pip3 install yagmail
6 編寫demo.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from unittest import mock
import time
def autodemo():
? ? # 實例化參數(shù)方法
? ? # chrome_options = Options()
? ? # 設置瀏覽器的無頭瀏覽器, 無界面, 瀏覽器將不提供界面, Linux操作系統(tǒng)無界面下就可以運行
? ? chrome_options = webdriver.ChromeOptions()
? ? chrome_options.add_argument('--headless')
? ? chrome_options.add_argument('--no-sandbox')
? ? chrome_options.add_argument('--disable-gpu')
? ? chrome_options.add_argument('--disable-dev-shm-usage')
? ? #chrome_options.add_argument('--disable-extensions')
? ? # 實例化chrome, 導入設置項
? ? # test_webdriver = webdriver.Chrome('/opt/google/chrome/chromedriver', options=chrome_options)
? ? test_webdriver = webdriver.Chrome(options=chrome_options)
? ? # test_webdriver = webdriver.Chrome('/opt/chromedriver/chromedriver', options=chrome_options)
? ? test_webdriver.minimize_window()
? ? test_webdriver.get("https://www.baidu.com")
? ? test_webdriver.find_element_by_xpath("http://input[@id='kw']").send_keys("python")
? ? test_webdriver.find_element_by_xpath("http://input[@id='su']").click()
? ? time.sleep(2)
? ? print(test_webdriver.title)
? ? test_webdriver.quit()
autodemo()
將demo.py 放/data/auto下面,執(zhí)行測試:
# python3 demo.py
7 install jenkins
# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo --no-check-certificate
# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
# yum upgrade
# yum install java-17-openjdk? ? if jdk has been installed correctly, do not run this
# yum install jenkins -y
# systemctl daemon-reload
# systemctl start jenkins? or #cd /etc/init.d? ? #./jenkins start
–no-sandbox –headless
創(chuàng)建自由風格項目,build step 加入:
cd /opt/auto
python3 demo.py
8 禁用Chrome自動更新
chrome版本更新,可能會導致驅動不兼容,可以考慮禁用chrome自動更新
# vim /usr/share/applications/google-chrome.desktop
找到Exec行,修改為:Exec=/usr/bin/google-chrome-stable --disable-component-update