From 0395dc22066325d09664833a434f23a433670a13 Mon Sep 17 00:00:00 2001 From: jsfehler Date: Fri, 24 May 2024 13:23:48 -0400 Subject: [PATCH] Don't throw exception when element.is_visible/is_not_visible can't find an element (#1277) --- splinter/driver/webdriver/__init__.py | 15 ++++++++--- tests/static/index.html | 7 +++++ tests/test_element_is_visible.py | 38 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/splinter/driver/webdriver/__init__.py b/splinter/driver/webdriver/__init__.py index ff4b53557..a4bf95db6 100644 --- a/splinter/driver/webdriver/__init__.py +++ b/splinter/driver/webdriver/__init__.py @@ -869,7 +869,10 @@ def is_visible(self, wait_time=None): def search() -> bool: # Element is refreshed to account for changes to the page. - self._refresh_element(wait_time=0) + try: + self._refresh_element(wait_time=0) + except ElementDoesNotExist: + return False try: result = self.visible @@ -890,12 +893,18 @@ def is_not_visible(self, wait_time=None): def search() -> bool: # Element is refreshed to account for changes to the page. - self._refresh_element(wait_time=0) + try: + self._refresh_element(wait_time=0) + + # If an element is not found at all, we assume it's not visible. + except ElementDoesNotExist: + return True try: result = self.visible # StaleElementReferenceException occurs if element is found - # but changes before visible is checked + # but changes before visible is checked. Retry the check + # because we don't know if it's truly not visible. except StaleElementReferenceException: return False diff --git a/tests/static/index.html b/tests/static/index.html index 74666b164..a8265e73e 100644 --- a/tests/static/index.html +++ b/tests/static/index.html @@ -85,6 +85,11 @@ $('.over-label').remove(); $('.over-input').remove(); }); + + setTimeout(function() { + $('#removed_after_5_seconds').remove(); + }, 5000 ); + }); @@ -220,5 +225,7 @@

inside

+ + diff --git a/tests/test_element_is_visible.py b/tests/test_element_is_visible.py index 7d72c0494..6ff7d27c3 100644 --- a/tests/test_element_is_visible.py +++ b/tests/test_element_is_visible.py @@ -1,6 +1,8 @@ # Copyright 2022 splinter authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. +import time + import pytest from .base import supported_browsers @@ -62,3 +64,39 @@ def test_element_is_not_visible_custom_wait_time(browser_name, get_new_browser): browser.visit(EXAMPLE_APP) assert browser.find_by_css("#invisible").is_not_visible(wait_time=12) + + +@pytest.mark.parametrize("browser_name", supported_browsers) +def test_element_is_visible_element_removed(browser_name, get_new_browser): + """ + Given an element has been found + When it is removed from the page + Then the is_visible() method for this element will return False + """ + browser = get_new_browser(browser_name) + browser.visit(EXAMPLE_APP) + + elem = browser.find_by_css("#removed_after_5_seconds") + + # Wait for the element to not be visible + time.sleep(5.5) + + assert not elem.is_visible() + + +@pytest.mark.parametrize("browser_name", supported_browsers) +def test_element_is_not_visible_element_removed(browser_name, get_new_browser): + """ + Given an element has been found + When it is removed from the page + Then the is_not_visible() method for this element will return True + """ + browser = get_new_browser(browser_name) + browser.visit(EXAMPLE_APP) + + elem = browser.find_by_css("#removed_after_5_seconds") + + # Wait for the element to not be visible + time.sleep(5.5) + + assert elem.is_not_visible()