ElementNotVisibleException: Message: element not visible


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

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

ElementNotVisibleException: Message: element not visible

 

原因:

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

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

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

    def find_element(self,*loc): """ 在指定时间内,查找元素;否则抛出异常 :param loc: 定位器 :return: 元素 或 抛出异常 """ TimeOut = 20
        try: self.driver.implicitly_wait(TimeOut) #智能等待;超时设置
 element = self.driver.find_element(*loc) #如果element没有找到,到此处会开始等待
            if self.isDisplayTimeOut(element,TimeOut): self.hightlight(element) #高亮显示
            else: raise ElementNotVisibleException #抛出异常,给except捕获
 self.driver.implicitly_wait(0) #恢复超时设置
            return element except (NoSuchElementException,ElementNotVisibleException,UnexpectedAlertPresentException) as ex: self.getImage raise ex

 

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

    def isDisplayTimeOut(self,element,timeSes): """ 在指定时间内,轮询元素是否显示 :param element: 元素对象 :param timeSes: 轮询时间 :return: bool """ start_time = int(time.time()) #秒级时间戳
        timeStr = int(timeSes) while (int(time.time())-start_time) <= timeSes: if element.is_displayed(): return True self.wait(500) self.getImage return False

 

 

 期待你的加入;共同学习,一起进步:
python|测试|技术交流 qq群:563227894
python|测试|技术交流 qq群:563227894
python|测试|技术交流 qq群:563227894


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM