3、常用基础方法二次封装
def get_img(self, filename):
"""获取截图"""
abpath = getcwds.get_cwds()
failimagepath = os.path.join(abpath, ‘reports\screenshots\fail‘)
passimagepath = os.path.join(abpath, ‘reports\screenshots\pass‘)
list_value = []
lists = filename.split(‘.‘)
for value in lists:
list_value.append(value)
if list_value[1] == ‘png‘ or list_value[1] == ‘jpg‘ or list_value[1] == ‘PNG‘ or list_value[1] == ‘JPG‘:
if ‘fail‘ in list_value[0].split(‘_‘):
try:
self.driver.save_screenshot(os.path.join(failimagepath, filename))
except Exception:
log.logger.exception(‘保存截图失败 !‘, exc_info=True)
else:
log.logger.info(
‘截图 [%s] 成功保存在 [%s]‘ % (filename, failimagepath))
elif ‘pass‘ in list_value[0]:
try:
self.driver.save_screenshot(os.path.join(passimagepath, filename))
except Exception:
log.logger.exception(‘保存截图失败 !‘, exc_info=True)
else:
log.logger.info(
‘截图 [%s] 成功保存在 [%s]‘ % (filename, passimagepath))
else:
log.logger.info(‘保存截图失败由于 [%s] 格式不正确‘ % filename)
else:
log.logger.info(
‘文件名 [%s] 格式不正确导致保存失败, 请检查!‘ % filename)
def find_element(self, selector):
"""定位页面元素位置的方法"""
by = selector[0]
value = selector[1]
element = None
try:
if by == ‘id‘ or by == ‘name‘ or by == ‘class‘ or by == ‘tag‘ or by == ‘link‘ or by == ‘plink‘ or by == ‘css‘ or by == ‘xpath‘:
if by == ‘id‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_id(value)))
elif by == ‘name‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_name(value)))
elif by == ‘class‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_class_name(value)))
elif by == ‘tag‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_tag_name(value)))
elif by == ‘link‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_link_text(value)))
elif by == ‘plink‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_partial_link_text(value)))
elif by == ‘css‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_css_selector(value)))
elif by == ‘xpath‘:
element = WebDriverWait(self.driver, 10, 0.5).until(
EC.visibility_of(self.driver.find_element_by_xpath(value)))
else:
raise NameError("请输入可查找的元素.")
return element
except Exception as e:
log.logger.exception(‘查找元素超时:%s‘ % selector[1], exc_info=True)
raise e
先到这里,以后用空再补充。我先去哭会。
原文:https://www.cnblogs.com/qingxingy/p/15090554.html