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

转载于:https://www.cnblogs.com/yhleng/p/9318845.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I'm sorry to hear that you're encountering a `NoSuchElementException` while using Selenium. This exception occurs when the element you're trying to locate or interact with cannot be found on the web page. To troubleshoot this issue, I suggest the following steps: 1. Verify the element locator: Double-check the correctness of the element locator (e.g., ID, class name, XPath, etc.) you are using to locate the element. Ensure that it accurately represents the element you're trying to interact with. 2. Wait for the element to appear: Sometimes, the element may take some time to load on the page. Implement an appropriate wait strategy like `WebDriverWait` to wait for the element to become visible or clickable before interacting with it. 3. Check if the element is present within an iframe: If the element you're trying to locate is within an iframe, you need to switch to that iframe context before locating the element. Use `driver.switch_to.frame()` method to switch to the correct iframe. 4. Ensure the element is not inside a shadow DOM: If the element is inside a shadow DOM, you won't be able to access it directly. You'll need to use `execute_script()` method to execute JavaScript code within the shadow DOM and perform your desired actions. 5. Verify if there are any dynamic elements: Some web pages dynamically load elements or change their structure after initial page load. If this is the case, ensure that you are locating the element after it has been fully loaded or after any dynamic changes have occurred. If none of these steps resolve the issue, providing more specific details about your code and the element you're trying to interact with would be helpful in further troubleshooting.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值