From c58ce4e52e11a61e01f3eb1a21c6e2482d7f7a27 Mon Sep 17 00:00:00 2001 From: Fenil Mehta <42742240+fenilgmehta@users.noreply.github.com> Date: Thu, 24 Nov 2022 09:47:35 +0530 Subject: [PATCH 1/2] Improve performance - reduce `find_element` calls --- .../webdriver/support/expected_conditions.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/py/selenium/webdriver/support/expected_conditions.py b/py/selenium/webdriver/support/expected_conditions.py index c4f9a243581c5..39aaa531b0233 100644 --- a/py/selenium/webdriver/support/expected_conditions.py +++ b/py/selenium/webdriver/support/expected_conditions.py @@ -234,10 +234,10 @@ def text_to_be_present_in_element_attribute(locator, attribute_, text_): def _predicate(driver): try: - if not element_attribute_to_include(locator, attribute_)(driver): + element = driver.find_element(*locator) + if not _element_has_attribute(element, attribute_): return False - element_text = driver.find_element(*locator).get_attribute(attribute_) - return text_ in element_text + return text_ in element.get_attribute(attribute_) except StaleElementReferenceException: return False @@ -414,6 +414,10 @@ def _predicate(driver): return _predicate +def _element_has_attribute(element, attribute_): + return element.get_attribute(attribute_) is not None + + def element_attribute_to_include(locator, attribute_): """An expectation for checking if the given attribute is included in the specified element. @@ -422,8 +426,8 @@ def element_attribute_to_include(locator, attribute_): def _predicate(driver): try: - element_attribute = driver.find_element(*locator).get_attribute(attribute_) - return element_attribute is not None + element = driver.find_element(*locator) + return _element_has_attribute(element, attribute_) except StaleElementReferenceException: return False From 13813d55e5dfbd12878c536a80911a9d57feead9 Mon Sep 17 00:00:00 2001 From: Fenil Mehta <42742240+fenilgmehta@users.noreply.github.com> Date: Thu, 24 Nov 2022 10:01:46 +0530 Subject: [PATCH 2/2] Reduce calls to `get_attribute` --- .../webdriver/support/expected_conditions.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/py/selenium/webdriver/support/expected_conditions.py b/py/selenium/webdriver/support/expected_conditions.py index 39aaa531b0233..71d9f0b841f0d 100644 --- a/py/selenium/webdriver/support/expected_conditions.py +++ b/py/selenium/webdriver/support/expected_conditions.py @@ -234,10 +234,10 @@ def text_to_be_present_in_element_attribute(locator, attribute_, text_): def _predicate(driver): try: - element = driver.find_element(*locator) - if not _element_has_attribute(element, attribute_): + element_text = driver.find_element(*locator).get_attribute(attribute_) + if element_text is None: return False - return text_ in element.get_attribute(attribute_) + return text_ in element_text except StaleElementReferenceException: return False @@ -414,10 +414,6 @@ def _predicate(driver): return _predicate -def _element_has_attribute(element, attribute_): - return element.get_attribute(attribute_) is not None - - def element_attribute_to_include(locator, attribute_): """An expectation for checking if the given attribute is included in the specified element. @@ -426,8 +422,8 @@ def element_attribute_to_include(locator, attribute_): def _predicate(driver): try: - element = driver.find_element(*locator) - return _element_has_attribute(element, attribute_) + element_attribute = driver.find_element(*locator).get_attribute(attribute_) + return element_attribute is not None except StaleElementReferenceException: return False