selenium--python重新整理多個頁面目標按鈕
阿新 • • 發佈:2018-11-22
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/