python webdriver关键字框架

关键字框架推荐使用eval,不使用exec

eval()函数有返回值,exec()函数无返回值

 

teststep.txt

open||chrome
visit||https://www.126.com
click||switchAccountLogin
sleep||3

 

主程序脚本

keyword.py

 

#encoding=utf-8
from selenium import webdriver
import time

with open("teststep.txt") as fp:
teststeps = fp.readlines()

driver = ""

def open(browser_name):
global driver
if "ie" in browser_name:
driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
elif "chrome" in browser_name:
driver = webdriver.Chrome(executable_path = "e:\\chromedriver")
else:
driver = webdriver.Firefox(executable_path = "e:\\geckodriver")

def visit(url):
global driver
driver.get(url)

def click(id):
try:
driver.find_element_by_id(id).click()
except:
print("click fail!")
raise

def sleep(times):
time.sleep(int(times))

for teststep in teststeps:
action = teststep.split("||")[0] #action = "open"
value= teststep.split("||")[1].strip() #value= "chrome"
try:
command = "%s(\"%s\")" %(action,value) # 拼接函数func(参数1,参数2)"open(\"chrome\")"
eval(command) #利用eval()执行函数 open("chrome")
except:
print("执行",command,"有异常")

print ("DONE!")

 

posted @ 2020-04-09 22:50  风声~~  阅读(378)  评论(0编辑  收藏  举报