selenium自动化测试中,经常会报异常:

可能会有各种疑问,元素可以定位到啊。为什么报以下异常?

ElementNotVisibleException: Message: element not visible

原因:

元素在前台代码document中可以找到,但是不代表该元素就显示在了页面上。

所以报上述异常的原因就是,元素未显示在界面上。

能过我们测试中会自定义一下,找元素的功能:

  1. def find_element(self,*loc):
  2. """
  3. 在指定时间内,查找元素;否则抛出异常
  4. :param loc: 定位器
  5. :return: 元素 或 抛出异常
  6. """
  7. TimeOut = 20
  8. try:
  9. self.driver.implicitly_wait(TimeOut) #智能等待;超时设置
  10.  
  11. element = self.driver.find_element(*loc) #如果element没有找到,到此处会开始等待
  12. if self.isDisplayTimeOut(element,TimeOut):
  13. self.hightlight(element) #高亮显示
  14. else:
  15. raise ElementNotVisibleException #抛出异常,给except捕获
  16.  
  17. self.driver.implicitly_wait(0) #恢复超时设置
  18. return element
  19.  
  20. except (NoSuchElementException,ElementNotVisibleException,UnexpectedAlertPresentException) as ex:
  21. self.getImage
  22. raise ex

判断元素是否在页面显示:

  1. def isDisplayTimeOut(self,element,timeSes):
  2. """
  3. 在指定时间内,轮询元素是否显示
  4. :param element: 元素对象
  5. :param timeSes: 轮询时间
  6. :return: bool
  7. """
  8. start_time = int(time.time()) #秒级时间戳
  9. timeStr = int(timeSes)
  10. while (int(time.time())-start_time) <= timeSes:
  11. if element.is_displayed():
  12. return True
  13. self.wait(500)
  14.  
  15. self.getImage
  16. return False
 期待你的加入;共同学习,一起进步:
python|测试|技术交流 qq群:563227894
python|测试|技术交流 qq群:563227894
python|测试|技术交流 qq群:563227894

ElementNotVisibleException: Message: element not visible的更多相关文章

  1. selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉菜单事件的处理

    使用Selenium爬虫时,可能会遇到一些下拉菜单,动态加载,如果直接使用find_element_by_函数会报错,显示selenium.common.exceptions.ElementNotVi ...

  2. 爬虫-Selenium -抱错ElementNotVisibleException: Message: element not visible

    1.当使用Selenium IDE 完成了脚本的录制和回放通过后,想要将脚本转换为其他语言如java.Python等,首次使用时打开Options->Format发现没有可以转换的语言,如下: ...

  3. Firefox上运行自动化测试脚本提示元素无法点击“WebDriverException: Message: Element is not clickable at point“解决方法

    1. Firefox上运行脚本时提示“WebDriverException: Message: Element is not clickable at point (934.316650390625, ...

  4. selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)

    今天给大家分享一个selenium中经常会有人遇到的坑: selenium.common.exceptions.StaleElementReferenceException: Message: Ele ...

  5. 两个标签页定位第二个标签页元素时显示element not visible

    问题描述 web页面有两个标签页, 当转换到第二个标签页定位元素时, 显示element not visible. 代码 ... //省略 WebElement ele= browser.getEle ...

  6. python+selenium:点击页面元素时报错:WebDriverException: Message: Element is not clickable at point (1372.5, 9.5). Other element would receive the click: <li style="display: list-item;" id="tuanbox"></li>

      遇到一个非常郁闷的问题,终于解决了, 问题是这样的,NN网站的价格计划,每一个价格计划需要三连击才能全部点开,第一个房型的价格计划是可以正确三连击打开的,可是第二个房弄就不行了,报错说不是可点击的 ...

  7. element not visible的解决方法

    抛出异常主题为element not visible主要有一下三个方面的原因. 元素之间存在逻辑关系,比如你要选择地址时,中国选择完毕之后,才能选择北京.如果想直接一步到位,则会出现element n ...

  8. python + web自动化,点击不生效,提示“selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (117, 674)”

    前言: 在做web自动化时,遇到一个缩放了浏览器比例的操作,从100%缩小到80%,再进行点击的时候,弹出报错信息,无法点击 selenium.common.exceptions.ElementCli ...

  9. selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)

    循环点击一列链接,但只能点到第一个,第二个失败,这是为什么,因为第二个已经是新页面,当然找不到之前页面的元素,虽然元素没变,甚至是后退回来,页面都没有变,为什么是新页面,页面长的一样不代表是同一张页面 ...

随机推荐

  1. {MySQL的库、表的详细操作}一 库操作 二 表操作 三 行操作

    MySQL的库.表的详细操作 MySQL数据库 本节目录 一 库操作 二 表操作 三 行操作 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf ...

  2. PHP-之POSIX系列函数和兼容Perl系列函数比较

    PHP有两种正则系列函数 POSIX 系列和兼容Perl系列的函数 在PHP大于5.3使用POSIX系列函数会报E_DEPRECATED 错误, POSIX系列函数在大于5.3版本不建议使用,PHP7 ...

  3. HZAU2018年十大阅读之星演讲稿

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  4. [tcpreplay] tcpreplay高级用法--使用tcpreplay-edit进行循环动态发包

    tcpreplay-edit提供了可对包进行修改的高级用法: --unique-ip Modify IP addresses each loop iteration to generate uniqu ...

  5. AndrewNG Deep learning课程笔记

    神经网络基础 Deep learning就是深层神经网络 神经网络的结构如下, 这是两层神经网络,输入层一般不算在内,分别是hidden layer和output layer hidden layer ...

  6. iOS更换科大讯飞的key

    我这个APP使用了科大讯飞的"语音评测"功能,之前使用的是我自己注册的科大讯飞账号,在这个账号里面创建的APP,生成的key. 我们公司有公司的key, 同事说可以多个APP公用一 ...

  7. python之路(转载)

    Python之路   Python生产环境部署(fastcgi,uwsgi)   Django   缓存.队列(Memcached.redis.RabbitMQ)   Python(九) Python ...

  8. tensorflow入门笔记(一) tf.app.flags.FLAGS

    tf.app.flags.DEFINE_xxx()就是添加命令行的optional argument(可选参数),而tf.app.flags.FLAGS可以从对应的命令行参数取出参数.举例如下: FL ...

  9. python编码类型互转总结

    1.只有在unicode下才能将utf-8与gbk互转2.unicode是在内存中使用,bytes是文件存储和网络传输时使用-------------------------------------- ...

  10. Java如何编写Servlet程序

    一:Servlet Servlet是Java服务器端编程,不同于一般的Java应用程序,Servlet程序是运行在服务器上的,服务器有很多种,Tomcat只是其中一种. 例子: 在Eclipse中新建 ...