Skip to content

Commit

Permalink
Catch stale element exception when checking visible/not visible status (
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler authored Sep 22, 2020
1 parent f0442d9 commit d83aa3f
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions stere/strategy/splinter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import copy
import typing

from selenium.common.exceptions import StaleElementReferenceException

from .strategy import strategy
from ..utils import _retry

Expand Down Expand Up @@ -66,10 +68,22 @@ def is_visible(self, wait_time: typing.Optional[int] = None) -> bool:
wait_time (int): The number of seconds to wait. If not specified,
Stere.retry_time will be used.
"""
return _retry(
lambda: self.find() and self.find().visible,
wait_time,
)
def search():
elem = self.find()
if elem:
try:
result = elem.visible
# StaleElementReferenceException occurs if element is found
# but changes before visible is checked
except StaleElementReferenceException:
return False

if result:
return True

return False

return _retry(search, wait_time)

def is_not_visible(self, wait_time: typing.Optional[int] = None) -> bool:
"""Check if an element is not visible in the DOM.
Expand All @@ -79,11 +93,20 @@ def is_not_visible(self, wait_time: typing.Optional[int] = None) -> bool:
Stere.retry_time will be used.
"""
def search():
result = self.find(wait_time=0)
if not result:
return True
if result and not result.visible:
elem = self.find(wait_time=0)
if elem:
try:
result = elem.visible
# StaleElementReferenceException occurs if element is found
# but changes before visible is checked
except StaleElementReferenceException:
return False

if not result:
return True
else:
return True

return False

return _retry(search, wait_time)
Expand Down

0 comments on commit d83aa3f

Please sign in to comment.