Python_Xpath应用_混合驱动(含正则)
Xpath应用_混合驱动(含正则)
一、混合模式(含正则)
数据文件&文件内容
testcse.txt#文件名
visit||http://www.bing.com
visit||${e:\\urls.txt}
代码内容
1、正则相关
>>> import re >>> >>> re.search(r"\$\{(.*?)\}","visit||${e:\\urls.txt}") <_sre.SRE_Match object; span=(7, 21), match='${e:\\urls.txt}'> >>> re.search(r"\$\{(.*?)\}","visit||${e:\\urls.txt}").group(1) 'e:\\urls.txt'
2、内容
from selenium import webdriver import time import re def visit(url): global driver driver.get(url) time.sleep(3) driver = webdriver.Chrome(executable_path="e:\\chromedriver") with open("e:\\testcase.txt") as fp: for line in fp: if re.search(r"\$\{(.*?)\}",line):#若是正则匹配的结果 data_file = re.search(r"\$\{(.*?)\}",line).group(1) with open(data_file) as fp: for data in fp: if data.strip()!="": action = line.split("||")[0] data = data.strip() print(action+"('"+data+"')") eval(action+"('"+data+"')") elif line.strip()!="":#若不是正则匹配的结果 action,url = line.split("||") url=url.strip() print(action,url,action+"('"+url+"')") eval(action+"('"+url+"')") #"visit('http://www.bing.com')" ''' with open("e:\\urls.txt") as fp: for line in fp: if line.strip()!="": driver.get(line) time.sleep(3) ''' driver.quit()
二、关键字驱动(含正则)
关键字驱动
所有动作都是分开的
from selenium import webdriver import time import re def visit(url): global driver driver.get(url) time.sleep(3) def assert_word(keyword): global driver assert keyword in driver.page_source print("断言'%s'在页面中存在成功" %keyword) driver = webdriver.Chrome(executable_path="e:\\chromedriver") with open("e:\\testcase.txt") as fp: for line in fp: if re.search(r"\$\{(.*?)\}",line): data_file = re.search(r"\$\{(.*?)\}",line).group(1) with open(data_file) as fp: for data in fp: if data.strip()!="": action = line.split("||")[0] data = data.strip() print(action+"('"+data+"')") eval(action+"('"+data+"')") elif line.strip()!="": action,url= line.split("||") url=url.strip() print(action,url,action+"('"+url+"')") eval(action+"('"+url+"')") #"visit('http://www.bing.com')" ''' with open("e:\\urls.txt") as fp: for line in fp: if line.strip()!="": driver.get(line) time.sleep(3) '''