selenium自動測試
阿新 • • 發佈:2018-11-21
import requests import sys import io from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import Select sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8') #改變標準輸出的預設編碼 browser = webdriver.Chrome('C:\chromedriver.exe') #登入頁面 url = r'http://192.168.102.103:8080/mu/' # 訪問登入頁面 browser.get(url) # 等待一定時間,讓js指令碼載入完畢 browser.implicitly_wait(3) #輸入使用者名稱 username = browser.find_element_by_id('username') username.send_keys('admin') #輸入密碼 password = browser.find_element_by_id('password') password.send_keys('11111') #點選“登入”按鈕login_button = browser.find_element_by_xpath('//td/img[1]') login_button.click() browser.switch_to.frame(browser.find_element_by_xpath('/html/frameset[1]/frameset[1]/frameset[1]/frameset[1]/frame[2]')) butten = browser.find_element_by_id('Bar_panel2_c') butten.click() butten = browser.find_element_by_xpath('//div[@id="Bar_panel2_b0"]/img[1]') actions = ActionChains(browser) actions.click(butten) actions.perform() browser.switch_to.default_content(); browser.switch_to.frame(browser.find_element_by_xpath('/html/frameset[1]/frameset[1]/frameset[2]/frameset[1]/frame[2]')) butten = browser.find_element_by_xpath('//table[@id="table0"]/tbody/tr[4]/td[1]/input[1]') butten.click() select = Select(browser.find_element_by_id('centerId')) select.select_by_index(1) editIP = browser.find_element_by_id('serverIp') editIP.send_keys('127.0.0.1') editname = browser.find_element_by_id('serverName') editname.send_keys('name') editname = browser.find_element_by_id('keepAlivePeriod') editname.send_keys('59') editloginname = browser.find_element_by_id('serverLoginName') editloginname.send_keys('admin') editlocation = browser.find_element_by_id('installLocation') editlocation.send_keys('安裝位置') editdesc = browser.find_element_by_id('description') editdesc.send_keys('裝置描述') #網頁截圖 browser.save_screenshot('C:/picture1.png') #列印網頁原始碼 print(browser.page_source.encode('utf-8').decode()) browser.implicitly_wait(10) browser.quit()