Skip to content

Commit

Permalink
expected_conditions and wait modules raises InvalidSelectorException …
Browse files Browse the repository at this point in the history
…for invalid xpaths (#9805)

Co-authored-by: David Burns <[email protected]>
  • Loading branch information
gpt14 and AutomatedTester authored Sep 24, 2021
1 parent b4c8f20 commit 10bcce1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
20 changes: 19 additions & 1 deletion py/selenium/webdriver/support/expected_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import re

from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchElementException, InvalidSelectorException
from selenium.common.exceptions import NoSuchFrameException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
Expand Down Expand Up @@ -123,6 +123,8 @@ def visibility_of_element_located(locator):
def _predicate(driver):
try:
return _element_if_visible(driver.find_element(*locator))
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

Expand Down Expand Up @@ -188,6 +190,8 @@ def _predicate(driver):
if _element_if_visible(element, visibility=False):
return False
return elements
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

Expand All @@ -204,6 +208,8 @@ def _predicate(driver):
try:
element_text = driver.find_element(*locator).text
return text_ in element_text
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

Expand All @@ -220,6 +226,8 @@ def _predicate(driver):
try:
element_text = driver.find_element(*locator).get_attribute("value")
return text_ in element_text
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

Expand All @@ -239,6 +247,8 @@ def _predicate(driver):
else:
driver.switch_to.frame(locator)
return True
except InvalidSelectorException as e:
raise e
except NoSuchFrameException:
return False

Expand All @@ -258,6 +268,8 @@ def _predicate(driver):
if not isinstance(target, WebElement):
target = driver.find_element(*target)
return _element_if_visible(target, False)
except InvalidSelectorException as e:
raise e
except (NoSuchElementException, StaleElementReferenceException):
# In the case of NoSuchElement, returns true because the element is
# not present in DOM. The try block checks if the element is present
Expand Down Expand Up @@ -312,6 +324,8 @@ def _predicate(_):
# Calling any method forces a staleness check
element.is_enabled()
return False
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return True

Expand Down Expand Up @@ -362,6 +376,8 @@ def _predicate(driver):
try:
element = driver.find_element(*locator)
return element.is_selected() == is_selected
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

Expand Down Expand Up @@ -407,6 +423,8 @@ def _predicate(driver):
try:
element_attribute = driver.find_element(*locator).get_attribute(attribute_)
return element_attribute is not None
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

Expand Down
6 changes: 5 additions & 1 deletion py/selenium/webdriver/support/wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.

import time
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchElementException, InvalidSelectorException
from selenium.common.exceptions import TimeoutException

POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method
Expand Down Expand Up @@ -78,6 +78,8 @@ def until(self, method, message=''):
value = method(self._driver)
if value:
return value
except InvalidSelectorException as e:
raise e
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
Expand All @@ -102,6 +104,8 @@ def until_not(self, method, message=''):
value = method(self._driver)
if not value:
return value
except InvalidSelectorException as e:
raise e
except self._ignored_exceptions:
return True
time.sleep(self._poll)
Expand Down
8 changes: 7 additions & 1 deletion py/test/selenium/webdriver/common/webdriverwait_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pytest

from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import TimeoutException, InvalidSelectorException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import InvalidElementStateException
Expand All @@ -32,6 +32,12 @@ def throwSERE(driver):
raise StaleElementReferenceException("test")


def testShouldFailWithInvalidSelectorException(driver, pages):
pages.load("dynamic.html")
with pytest.raises(InvalidSelectorException):
WebDriverWait(driver, 0.7).until(EC.presence_of_element_located((By.XPATH, "//*[contains(@id,'something'")))


def testShouldExplicitlyWaitForASingleElement(driver, pages):
pages.load("dynamic.html")
add = driver.find_element(By.ID, "adder")
Expand Down

0 comments on commit 10bcce1

Please sign in to comment.