python模拟登录并操作网页

6 篇文章 0 订阅
1 篇文章 0 订阅

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++
# python安装包下载安装
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
# 添加python路径
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安装selenium
pip install selenium
find / -name *pip*
# /opt/python39/bin/ 目录下有pip、pip3和pip3.9其实它们三个是一模一样的
/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

# 安装chromedriver一定要注意和chrome的版本相对应
# 查看chrome版本信息
/opt/google/chrome/chrome --version
Google Chrome 91.0.4472.77 unknown
# 可以在这个网站去找对应的驱动版本 http://npm.taobao.org/mirrors/chromedriver/
wget http://npm.taobao.org/mirrors/chromedriver/91.0.4472.19/chromedriver_linux64.zip
# 下载下来是一个zip包,系统没有unzip命令就安装一个
yum install unzip -y
# 解压文件后得到驱动文件,并将驱动文件放入python的路径下
unzip chromedriver_linux64.zip 
mv chromedriver /opt/python39/bin/

3、编写python代码

# 编写python脚本
vim chrome_seleuim.py
# 执行命令
/opt/python39/bin/python3 /opt/chrome_seleuim.py
#!/opt/python39/bin/python3
# encoding=utf-8
import time
from selenium import webdriver

# 创建chrome参数对象
options = webdriver.ChromeOptions()
# 解决DevToolsActivePort文件不存在的报错
options.add_argument('--no-sandbox') 
# 指定浏览器分辨率
options.add_argument('window-size=1600x900') 
# 谷歌文档提到需要加上这个属性来规避bug
options.add_argument('--disable-gpu') 
# 隐藏滚动条, 应对一些特殊页面
options.add_argument('--hide-scrollbars') 
# 不加载图片, 提升速度
options.add_argument('blink-settings=imagesEnabled=false') 
# 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
options.add_argument('--headless') 

# 初始化一个driver,chromedriver的路径选择的是相对路径,不行就写绝对路径
driver = webdriver.Chrome(options=options,executable_path='./chromedriver')

# 网页主页面请求路径
url = "http://xxx.xxx.xx.xxxx:8080/xxxxxx/main.do?method=login"
# 请求网页
driver.get(url)
# 通过page_source获取网页源代码,定位到输入框,并且输入登录账号信息,点击登录
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()

#time.sleep(2)
# 打开需要填写信息的表单页面
url2 = 'http://xxx.xxx.xxx.xxx:8080/xxxxxx/collaboration/collaboration.do?method=newColl&from=bizconfig&firstName=4382912957634034094'
driver.get(url2)
time.sleep(1)

# 设置表单数据提交信息 
# 本网页使用了iframe标签,必须要使用switch_to.frame()方法进行切换,不然页面元素无法定位
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()
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值