import yaml
defyaml_read(file_path):"""read"""withopen(file_path,'r',encoding='utf-8')as f:
x = yaml.load(f,Loader=yaml.FullLoader)return x
defyaml_a_write(file_path,data):"""追加写入"""
f =open(file_path,'a',encoding='utf-8')
yaml.dump(data,f,allow_unicode=True)
f.close()defyaml_w_write(file_path,data):"""覆盖写入"""
f =open(file_path,'w',encoding='gbk')
yaml.dump(data,f,allow_unicode=True)
f.close()defyaml_dict_update(file_path,dict_data):"""对yaml(only for dict)进行更新"""try:
x = yaml_read(file_path)ifnot x:
x =dict()
x.update(dict_data)
yaml_w_write(file_path,x)except:print(f'{file_path}<<<<<{dict_data}更新失败!!!')raiseif __name__ =='__main__':
res = yaml_read(r"E:\pythonProject\ZrlogWebUI\config\config.yaml")print(res)
import yaml
defyaml_read(file_path):"""read"""withopen(file_path,'r',encoding='utf-8')as f:
x = yaml.load(f,Loader=yaml.FullLoader)return x
defyaml_a_write(file_path,data):"""追加写入"""
f =open(file_path,'a',encoding='utf-8')
yaml.dump(data,f,allow_unicode=True)
f.close()defyaml_w_write(file_path,data):"""覆盖写入"""
f =open(file_path,'w',encoding='gbk')
yaml.dump(data,f,allow_unicode=True)
f.close()defyaml_dict_update(file_path,dict_data):"""对yaml(only for dict)进行更新"""try:
x = yaml_read(file_path)ifnot x:
x =dict()
x.update(dict_data)
yaml_w_write(file_path,x)except:print(f'{file_path}<<<<<{dict_data}更新失败!!!')raiseif __name__ =='__main__':
res = yaml_read(r"E:\pythonProject\ZrlogWebUI\config\config.yaml")print(res)
base_06_webUI脚本优化.py
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from base.chrome_options import chrome_options
from base.yaml_tools import yaml_read
from urllib.parse import urljoin
classBase:def__init__(self,config_file_path):
self.config_dict = yaml_read(config_file_path)
option = chrome_options()ifint(self.config_dict['headless'])==1:
option.add_argument("--headless")
self.driver = webdriver.Chrome(chrome_options=chrome_options())
self.driver.implicitly_wait(5)# 隐式等待5秒defopen(self, data_dict):"""打开网址"""
base_url = self.config_dict[data_dict['locate_by']]
self.driver.get(urljoin(base_url,data_dict['txt']))deflocate(self, data_dict):"""定位,应用显式等待""""""
WebDriverWait(driver,timeout,poll_frequency,ignored_exceptions)
driver:浏览器驱动
timeout:最长超时时间,默认以秒为单位。
poll_frequency:检测的间隔步长,默认为0.5s。即每0.5s询问一次,元素是否找到。
ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常。
"""try:
ele = WebDriverWait(self.driver,6,0.5).until(lambda s: s.find_element(data_dict['locate_by'], data_dict['locate_value']))# 显式等待# ele = self.driver.find_element(ele_type,ele_value)return ele
except:print("定位失败")raisedefinput(self, data_dict):"""输入"""
self.locate(data_dict).send_keys(data_dict['txt'])# self.driver.find_element(ele_type,ele_value).send_keys(txt)defclick(self, data_dict):"""单击"""
self.locate(data_dict).click()# self.driver.find_element(ele_type,ele_value).click()defmax_window(self, data_dict):"""窗口最大化"""
self.driver.maximize_window()defswitch_to_iframe(self, data_dict):"""切换到iframe"""
self.driver.switch_to.frame(data_dict['txt'])defswitch_to_windows(self, data_dict):"""切换新窗口"""
windows = self.driver.window_handles
self.driver.switch_to.window(windows[int(data_dict['txt'])])# 指定切换哪一页面,-1为最新页面defassert_locate(self, data_dict):"""断言"""assert self.locate(data_dict)isnotNone,'断言失败'print('断言成功!')defwait(self, data_dict):"""固定等待"""
sleep(int(data_dict['txt']))defquit(self, data_dict):"""退出浏览器"""
self.driver.quit()