日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python selenium框架_基于python+selenium的框架思路

發(fā)布時(shí)間:2025/3/20 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python selenium框架_基于python+selenium的框架思路 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

設(shè)想:

1、使用excel編寫用例第一個(gè)sheet頁為用例概要格式如下:

后面的sheet頁為具體的用例步驟:

實(shí)現(xiàn)所有定位信息都與測(cè)試代碼分離

2、讀取該excel文件取出關(guān)鍵字等信息,作為關(guān)鍵字的參數(shù),通過反射機(jī)制傳遞給關(guān)鍵字方法去執(zhí)行。

關(guān)鍵字模塊如下:ObjectMap.py

# coding:utf-8

from selenium.webdriver.support.ui import WebDriverWait

#獲取單個(gè)頁面元素對(duì)象

def get_element(driver, locationType, locatorExpression):

try:

element = WebDriverWait(driver, 30).until(lambda x:x.find_element(by=locationType,value = locatorExpression))

return element

except Exception, e:

raise e

def get_elements(driver , locationType, locatorExpression):

try:

elements = WebDriverWait(driver, 30).until(lambda x:x.find_elements(by=locationType,value=locatorExpression))

return elements

except Exception, e:

raise e

# 由于關(guān)鍵字函數(shù)的參數(shù)個(gè)數(shù)不一樣,所以通過傳遞動(dòng)態(tài)參數(shù)*args實(shí)現(xiàn)傳參,關(guān)鍵字方法

# 最多需要(driver , locationType, locatorExpression, operationValue)四個(gè)參數(shù)

def open_browser(driver, *args):

driver.get(args[2])

def input_string(driver, *args):

WebDriverWait(driver, 30).until(lambda x: x.find_element(by=args[0], value=args[1])).send_keys(args[2])

def click(driver, *args):

WebDriverWait(driver, 30).until(lambda x: x.find_element(by=args[0], value=args[1])).click()

測(cè)試執(zhí)行代碼如下:

# coding:utf-8

from util import ObjectMap, ExcelUtil

import xlrd, xlwt

import time

from xlutils.copy import copy

def baidu_search():

#初始化操作,創(chuàng)建driver

from selenium import webdriver

start_time = time.time()

# print start_time

driver = webdriver.Chrome()

#讀取excel中的關(guān)鍵字的值,定位方式的值,定位表達(dá)式,和操作值等參數(shù)值。然后將參數(shù)值傳到對(duì)應(yīng)關(guān)鍵字方法中

excelFile = xlrd.open_workbook(r"D:\KeyWordsFrameWork\testScripts\search.xlsx", formatting_info=True)

sheet = excelFile.sheet_by_index(1)

maxRows = sheet.nrows

# print maxRows

for row in range(1, maxRows-1):

keyword = sheet.row_values(row)[2]

locationType = sheet.row_values(row)[3]

locatorExpression = sheet.row_values(row)[4]

operationValue = sheet.row_values(row)[5]

# dir(ObjectMap)獲取該模塊的所有方法和變量

# print dir(ObjectMap)

for i in dir(ObjectMap):

if keyword == i:

# print i

# 要用到反射機(jī)制,通過函數(shù)名字符串調(diào)用對(duì)應(yīng)方法:http://www.liujiangblog.com/course/python/48

if hasattr(ObjectMap, keyword):

# print ‘有這個(gè)方法‘

func = getattr(ObjectMap, keyword)

func(driver, locationType, locatorExpression, operationValue)

end_time = time.time()

take_time = end_time-start_time

print take_time

excleFileCopy = copy(excelFile)

case_sheet = excleFileCopy.get_sheet(0)

case_sheet.write(1,5,take_time)

excleFileCopy.save(r"D:\KeyWordsFrameWork\testScripts\search.xlsx")

if __name__ == ‘__main__‘:

baidu_search()

總結(jié)

以上是生活随笔為你收集整理的python selenium框架_基于python+selenium的框架思路的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。