当前位置: 首页 > news >正文

python+selenium-UI自动框架之[优化]元素查找和BasePage页面

痛点:在页面查找元素的时候会遇到找不到或者其他无法处理某个字段的情况,又或者想要在输出的log或者report里面显示这个字段名称,这时候加上字段名称就很重要!

[3]python+selenium - UI自动框架之封装查找元素icon-default.png?t=N7T8https://mp.csdn.net/mp_blog/creation/editor/139117915对上面一篇文章做优化

elementFind.py

from typing import Optional, Union
from selenium.common import TimeoutException, StaleElementReferenceException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from public.log import logclass ElementFinder(object):def __init__(self, driver: WebDriver):self._driver = driverdef find_element(self, by: Union[By, str], value: str, display_name: Optional[str] = None,  time: int = 5, internal=0.5):_locator = (by, value)if display_name is None:display_name = valuefor _ in range(2):log.info('[100]:find element: {ele}'.format(ele=display_name))try:_element = WebDriverWait(self._driver, time, internal).until(ec.presence_of_element_located(_locator))return _elementexcept TimeoutException:log.exception('[400]:%s not find ' % display_name)except StaleElementReferenceException as e:log.exception('[401]:StaleElementReferenceException %s' % e)continueexcept NoSuchElementException as e:log.exception('[402]: NoSuchElement %s' % e)except Exception as e:log.exception('[403]:unknown error %s' % e)break

举个例子:

    inputBox = ('id', 'kw','input box')searchBtn = ('id', 'su', 'search button')

Note:元素定位的时候,可以加上字段的’名称‘,当然也可以不加,这是一个非必要参数

相应地,在BasePage页面调用查找元素函数的时候也需要做改动,因为传给查找元素的参数多了一个字段。

[4]python+selenium - UI自动框架之封装基类BasePage页面icon-default.png?t=N7T8https://mp.csdn.net/mp_blog/creation/editor/140102485只对basePage里面的一个功能做改动,其他类似要自己手动改:

basePage.py

from selenium.common import WebDriverException
from public.elementFind import ElementFinder
from public.log import logclass BasePage(ElementFinder):def click(self, locator):_element = self.find_element(*locator)if not _element:log.warning('[400]element {ele} not exits'.format(ele=locator))for _ in range(2):try:_element.click()breakexcept WebDriverException as e:log.exception('[401]:click {ele} failed,{info}'.format(ele=locator, info=e))except Exception as e:log.warning('[404]:click {ele},unknown error:{error}'.format(ele=locator, error=e))raise 'StopTestCaseException'

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 跨域问题出现的原因,怎么解决?
  • 《Windows API每日一练》9.25 系统菜单
  • Ubuntu18.04安装ROS
  • 【解密】记一次辽宁省某综合实践教学管理平台加解密算法分析
  • Proteus元器件库与仪器仪表
  • excel、word、ppt 下载安装步骤整理
  • 网络编程笔记
  • 电影购票小程序论文(设计)开题报告
  • 原创音乐小程序的设计
  • JVM:SpringBoot TomcatEmbeddedWebappClassLoader
  • 深度学习-4-PyTorch中的数据加载器Dataset和DataLoader
  • Paddle 打包部署
  • Vue 3中使用 Lottie 动画
  • python自动化之validator验证数据【代码示例】
  • STM32智能停车场管理系统教程
  • 2017 前端面试准备 - 收藏集 - 掘金
  • canvas 绘制双线技巧
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • java多线程
  • Logstash 参考指南(目录)
  • zookeeper系列(七)实战分布式命名服务
  • 多线程事务回滚
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 微信开放平台全网发布【失败】的几点排查方法
  • 一文看透浏览器架构
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 运行时添加log4j2的appender
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • ​2020 年大前端技术趋势解读
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • (16)Reactor的测试——响应式Spring的道法术器
  • (7)STL算法之交换赋值
  • (BFS)hdoj2377-Bus Pass
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (vue)页面文件上传获取:action地址
  • (二)延时任务篇——通过redis的key监听,实现延迟任务实战
  • (回溯) LeetCode 78. 子集
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (四)汇编语言——简单程序
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .net 7和core版 SignalR
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .Net环境下的缓存技术介绍
  • .NET轻量级ORM组件Dapper葵花宝典
  • @ComponentScan比较
  • @EnableWebMvc介绍和使用详细demo
  • @Not - Empty-Null-Blank
  • [ 蓝桥杯Web真题 ]-布局切换
  • [20170713] 无法访问SQL Server