Skip to content

Commit

Permalink
Don't throw exception when element.is_visible/is_not_visible can't fi…
Browse files Browse the repository at this point in the history
…nd an element (#1277)
  • Loading branch information
jsfehler authored May 24, 2024
1 parent 8aa8662 commit 0395dc2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
15 changes: 12 additions & 3 deletions splinter/driver/webdriver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions tests/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
$('.over-label').remove();
$('.over-input').remove();
});

setTimeout(function() {
$('#removed_after_5_seconds').remove();
}, 5000 );

});
</script>
</head>
Expand Down Expand Up @@ -220,5 +225,7 @@ <h2>inside</h2>

<div id='has_shadow_root'></div>
<div id="zerodiv" style="width: 0px; height: 0px;"></div>

<button id="removed_after_5_seconds">This button will be removed in 5 seconds</button>
</body>
</html>
38 changes: 38 additions & 0 deletions tests/test_element_is_visible.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 0395dc2

Please sign in to comment.