selenium–python刷新多個頁面目標按鈕
需求:有個監控系統,需要手動點擊所有的系統頁面,點擊獲取每個數據庫的統計信息時間,這一動作,根據自動化功能解放勞動力目的地,在學習python的spider中發現自動化測試框架–selenium可以模擬人的點擊輸入動作,特作此腳本
優秀:
- 遍歷每個頁面系統,遍歷點擊每個頁面
- 在find_element_by_xpath中加入參數
- 特別注意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/