1、需求介绍
1.1、功能呈现
1.1.1、模拟打开浏览器系统,并登录页面
1.1.2、连接到需要操作的表单页面
1.1.3、定位到需要的表单字段,自动填充信息
1.1.4、定位到提交按钮,点击提交信息
1.1.5、关闭浏览器,结束
1.2、相关技术流程分析
1.2.1、本次使用最小化安装操作系统CentOS Linux release 7.9.2009 (Core),语言python3.95,selenium技术框架自动化操作浏览器,浏览器选用chrome,配合chromedriver使用
2.、安装相关系统工具
2.1、python安装
yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
tar -zxvf Python-3.9.5.tgz
cd Python-3.9.5
./configure --prefix=/opt/python39
make && make install
vim /etc/profile
PATH=/opt/python39/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile
python3 --version
Python 3.9.5
2.2、安装Selenium
pip install selenium
find / -name *pip*
/opt/python39/bin/pip3 install selenium
/opt/python39/bin/python3.9 -m pip install --upgrade pip
2.3、安装chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
rpm -ivh google-chrome-stable_current_x86_64.rpm
yum install epel-release -y
yum provides */libappindicator3.so.1
yum install -y libappindicator-gtk3-12.10.0-11.el7.x86_64
yum install -y libappindicator-gtk3-12.10.0-13.el7.i686
yum -y install liberation-fonts
yum install alsa-lib-devel -y
yum provides */libvulkan.so.1
yum -y install vulkan-1.1.97.0-1.el7.x86_64
yum search xdg-utils
yum install xdg-utils -y
rpm -ivh google-chrome-stable_current_x86_64.rpm
2.4、安装chromedriver
/opt/google/chrome/chrome --version
Google Chrome 91.0.4472.77 unknown
wget http://npm.taobao.org/mirrors/chromedriver/91.0.4472.19/chromedriver_linux64.zip
yum install unzip -y
unzip chromedriver_linux64.zip
mv chromedriver /opt/python39/bin/
3、编写python代码
vim chrome_seleuim.py
/opt/python39/bin/python3 /opt/chrome_seleuim.py
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('window-size=1600x900')
options.add_argument('--disable-gpu')
options.add_argument('--hide-scrollbars')
options.add_argument('blink-settings=imagesEnabled=false')
options.add_argument('--headless')
driver = webdriver.Chrome(options=options,executable_path='./chromedriver')
url = "http://xxx.xxx.xx.xxxx:8080/xxxxxx/main.do?method=login"
driver.get(url)
login_username = driver.find_element_by_id('login_username')
login_username.send_keys('admin')
login_password = driver.find_element_by_id('login_password')
login_password.send_keys('123456')
login_button = driver.find_element_by_id('login_button')
login_button.click()
url2 = 'http://xxx.xxx.xxx.xxx:8080/xxxxxx/collaboration/collaboration.do?method=newColl&from=bizconfig&firstName=4382912957634034094'
driver.get(url2)
time.sleep(1)
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="zwIframe"]'))
driver.find_element_by_xpath('/html/body/div/section/div[3]/div/section/div[2]/table/tr[4]/td[3]/div/div/section/div[2]/div/input').send_keys(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
time.sleep(3)
driver.switch_to.parent_frame()
driver.find_element_by_id("sendId_a").click()
time.sleep(1)
driver.close()
driver.quit()