selenium--python刷新多个页面目标按钮

selenium–python刷新多个页面目标按钮


需求:有个监控系统,需要手动点击所有的系统页面,点击获取每个数据库的统计信息时间,这一动作,根据自动化功能解放劳动力目的地,在学习python的spider中发现自动化测试框架–selenium可以模拟人的点击输入动作,特作此脚本

优秀:

  1. 遍历每个页面系统,遍历点击每个页面
  2. 在find_element_by_xpath中加入参数
  3. 特别注意xx_elements_by_xxx与xx_element_by_xxx的区别,获得的结果也不同,s的结果是list,另外的结果是object

selenium 知识

常用的知识点:点击,输入,回退,获取新页面
点击: .click() .send_keys(Keys.ENTER)
clear 清除元素的内容
send_keys 模拟按键输入
submit 提交表单
输入: .send_keys(‘文本信息’)
回退:driver.back()
获取新页面:driver.current_window_handle
难点:获取各种页面元素,我就用过findid和xpath,特别喜欢用xpath,因为在spider中用过lxml中有xpath的语法,且在代码中看看;

上代码:

import csv
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from lxml import etree

##新目的,自动刷新所有系统的schema
#打开网址
driver=webdriver.Firefox()
try:
	driver.get('http://8.1.8.148/login/')
	#将浏览器最大化显示
	driver.maximize_window()
	time.sleep(2)
	# driver.find_element_by_xpath('//input[@placeholder="用户名"]').click()
	# driver.find_element_by_xpath('//input[@placeholder="用户名"]').send_keys('admin')
	driver.find_element_by_id('username').click()
	driver.find_element_by_id('username').send_keys('admin')
	driver.find_element_by_id('password').click()
	driver.find_element_by_id('password').send_keys('password')
	driver.find_element_by_xpath('//button[@type="button"]').click()
	time.sleep(5)
	# 获取当前页
	driver.current_window_handle
	#点击数据源
	driver.find_element_by_xpath('//div[@class="ant-layout-sider-children"]/ul/li[2]').click()
	time.sleep(2)
	#关于下一页的点击动作
#	#获取下一页的数字标签,只获取一个数据,结尾的数据,从1开始,使用跳转输入框
#	#失败了
#	pages=driver.find_elements_by_xpath('//div[@class="ant-spin-container"]/ul/li')
#	endnum=int(len(pages)-2)
#	#print(len(pages),endnum,type(endnum))
#	endpage=driver.find_element_by_xpath('//div[@class="ant-spin-container"]/ul/li[%s]/a' % endnum).text
#	#print(endpage)
#	#循环下一页
#	for pagenum in range(1,int(endpage)+1):
#		if pagenum != 1:
#			driver.find_element_by_xpath('//div[@class="ant-pagination-options-quick-jumper"]/input').clear()
#			driver.find_element_by_xpath('//div[@class="ant-pagination-options-quick-jumper"]/input').send_keys(pagenum)
#			driver.find_element_by_xpath('//div[@class="ant-pagination-options-quick-jumper"]/input').send_keys(Keys.ENTER)
#			time.sleep(2)
#		time.sleep(2)

	#关于下一页的点击动作
	#一个一个点击下一页标签
	pages=driver.find_elements_by_xpath('//div[@class="ant-spin-container"]/ul/li')
	for page in range(1,int(len(pages))):
		if page ==1 or page>(int(len(pages))-2):
			continue
		if page != 2 :
			driver.find_element_by_xpath('//div[@class="ant-spin-container"]/ul/li[%s]' % page).click()
			time.sleep(2)
		# 获取当前页
		driver.current_window_handle
		systemlists=driver.find_elements_by_xpath('//tbody[@class="ant-table-tbody"]/tr')
		print('第',page-1,'页系统个数:',len(systemlists))
		time.sleep(2)
		#循环系统个数
		for i in range(1,len(systemlists)+1):
			# 获取当前页
			driver.current_window_handle
			systemname= driver.find_element_by_xpath('//tbody[@class="ant-table-tbody"]/tr[%s]/td[2]/a' % i).text
			print('第',page-1,'页','第',i,'个系统名称:',systemname)
			driver.find_element_by_xpath('//tbody[@class="ant-table-tbody"]/tr[%s]/td[2]/a' % i).click()
			time.sleep(5)
			# 获取当前页
			driver.current_window_handle
			lis=driver.find_elements_by_xpath('//div[@class="ant-layout-sider-children"]/ul/li')
			#print(len(lis))
			#循环系统的子标签
			for j in range(1,len(lis)-1):
				try:
					titlename=driver.find_element_by_xpath('//div[@class="ant-layout-sider-children"]/ul/li[%s]/a' % j).text
					#print('系统中的标签名称:',titlename)
					if titlename=='Schema':
						print('点击',systemname,'系统的',titlename,'标签')
						driver.find_element_by_xpath('//div[@class="ant-layout-sider-children"]/ul/li[%s]' % j).click()
						time.sleep(5)
						# 获取当前页
						driver.current_window_handle
						driver.find_element_by_xpath('//section[@class="mW5r7M2EyUR4k69iYJpfC"]/section/button').click()
						print('刷新',systemname,'系统的schema')
						time.sleep(5)
						break
				except:
					continue
			driver.back()
			time.sleep(2)
			driver.back()
			time.sleep(2)

	print('所有系统的schema一刷新完毕,脚本正常退出')
finally:
	driver.close()

执行结果:
附上录制视频连接,上传在b站上:
https://www.bilibili.com/video/av34090669/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章