Skip to content

Commit

Permalink
SELENIUM_EX: allow ignore stale element on based actions if need
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Trifonov committed Aug 31, 2016
1 parent 509d9cd commit 225b1c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion seismograph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from .scope import set_default_program_layers


__version__ = '0.3.20'
__version__ = '0.3.21'


VERSION = tuple(map(int, __version__.split('.')))
Expand Down
2 changes: 2 additions & 0 deletions seismograph/ext/selenium/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def __init__(self, selenium, driver):
'MAXIMIZE_WINDOW', DEFAULT_MAXIMIZE_WINDOW,
)

self.IGNORE_STALE_ELEMENT = selenium.config.get('IGNORE_STALE_ELEMENT', False)

@property
def PAGE_LOAD_TIMEOUT(self):
return self.__PAGE_LOAD_TIMEOUT
Expand Down
29 changes: 29 additions & 0 deletions seismograph/ext/selenium/proxy/interfaces/webelement.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
# -*- coding: utf-8 -*-

from warnings import warn
from functools import wraps

from selenium.common.exceptions import StaleElementReferenceException

from .....utils import pyv
from .base import BaseInterface


def allow_ignore_stale(f):
@wraps(f)
def wrapper(self, *args, **kwargs):
if self.config.IGNORE_STALE_ELEMENT:
try:
return f(self, *args, **kwargs)
except StaleElementReferenceException as error:
warn(
'StaleElementReferenceException: {}'.format(
pyv.get_exc_message(error)
),
RuntimeWarning,
)
return
return f(self, *args, **kwargs)
return wrapper


class WebElementInterface(BaseInterface):

def __hash__(self, *args, **kwargs):
Expand Down Expand Up @@ -60,12 +84,15 @@ def _execute(self, *args, **kwargs):
def _upload(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('_upload')(*args, **kwargs)

@allow_ignore_stale
def click(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('click')(*args, **kwargs)

@allow_ignore_stale
def submit(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('submit')(*args, **kwargs)

@allow_ignore_stale
def clear(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('clear')(*args, **kwargs)

Expand Down Expand Up @@ -126,9 +153,11 @@ def find_element_by_css_selector(self, *args, **kwargs):
def find_elements_by_css_selector(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('find_elements_by_css_selector')(*args, **kwargs)

@allow_ignore_stale
def send_keys(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('send_keys')(*args, **kwargs)

@allow_ignore_stale
def set(self, *args, **kwargs):
return self.__getattr_from_webdriver_or_webelement__('send_keys')(*args, **kwargs)

Expand Down

0 comments on commit 225b1c5

Please sign in to comment.