Skip to content

Commit 10bcce1

Browse files
expected_conditions and wait modules raises InvalidSelectorException for invalid xpaths (#9805)
Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
1 parent b4c8f20 commit 10bcce1

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

py/selenium/webdriver/support/expected_conditions.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import re
1919

20-
from selenium.common.exceptions import NoSuchElementException
20+
from selenium.common.exceptions import NoSuchElementException, InvalidSelectorException
2121
from selenium.common.exceptions import NoSuchFrameException
2222
from selenium.common.exceptions import StaleElementReferenceException
2323
from selenium.common.exceptions import WebDriverException
@@ -123,6 +123,8 @@ def visibility_of_element_located(locator):
123123
def _predicate(driver):
124124
try:
125125
return _element_if_visible(driver.find_element(*locator))
126+
except InvalidSelectorException as e:
127+
raise e
126128
except StaleElementReferenceException:
127129
return False
128130

@@ -188,6 +190,8 @@ def _predicate(driver):
188190
if _element_if_visible(element, visibility=False):
189191
return False
190192
return elements
193+
except InvalidSelectorException as e:
194+
raise e
191195
except StaleElementReferenceException:
192196
return False
193197

@@ -204,6 +208,8 @@ def _predicate(driver):
204208
try:
205209
element_text = driver.find_element(*locator).text
206210
return text_ in element_text
211+
except InvalidSelectorException as e:
212+
raise e
207213
except StaleElementReferenceException:
208214
return False
209215

@@ -220,6 +226,8 @@ def _predicate(driver):
220226
try:
221227
element_text = driver.find_element(*locator).get_attribute("value")
222228
return text_ in element_text
229+
except InvalidSelectorException as e:
230+
raise e
223231
except StaleElementReferenceException:
224232
return False
225233

@@ -239,6 +247,8 @@ def _predicate(driver):
239247
else:
240248
driver.switch_to.frame(locator)
241249
return True
250+
except InvalidSelectorException as e:
251+
raise e
242252
except NoSuchFrameException:
243253
return False
244254

@@ -258,6 +268,8 @@ def _predicate(driver):
258268
if not isinstance(target, WebElement):
259269
target = driver.find_element(*target)
260270
return _element_if_visible(target, False)
271+
except InvalidSelectorException as e:
272+
raise e
261273
except (NoSuchElementException, StaleElementReferenceException):
262274
# In the case of NoSuchElement, returns true because the element is
263275
# not present in DOM. The try block checks if the element is present
@@ -312,6 +324,8 @@ def _predicate(_):
312324
# Calling any method forces a staleness check
313325
element.is_enabled()
314326
return False
327+
except InvalidSelectorException as e:
328+
raise e
315329
except StaleElementReferenceException:
316330
return True
317331

@@ -362,6 +376,8 @@ def _predicate(driver):
362376
try:
363377
element = driver.find_element(*locator)
364378
return element.is_selected() == is_selected
379+
except InvalidSelectorException as e:
380+
raise e
365381
except StaleElementReferenceException:
366382
return False
367383

@@ -407,6 +423,8 @@ def _predicate(driver):
407423
try:
408424
element_attribute = driver.find_element(*locator).get_attribute(attribute_)
409425
return element_attribute is not None
426+
except InvalidSelectorException as e:
427+
raise e
410428
except StaleElementReferenceException:
411429
return False
412430

py/selenium/webdriver/support/wait.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717

1818
import time
19-
from selenium.common.exceptions import NoSuchElementException
19+
from selenium.common.exceptions import NoSuchElementException, InvalidSelectorException
2020
from selenium.common.exceptions import TimeoutException
2121

2222
POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method
@@ -78,6 +78,8 @@ def until(self, method, message=''):
7878
value = method(self._driver)
7979
if value:
8080
return value
81+
except InvalidSelectorException as e:
82+
raise e
8183
except self._ignored_exceptions as exc:
8284
screen = getattr(exc, 'screen', None)
8385
stacktrace = getattr(exc, 'stacktrace', None)
@@ -102,6 +104,8 @@ def until_not(self, method, message=''):
102104
value = method(self._driver)
103105
if not value:
104106
return value
107+
except InvalidSelectorException as e:
108+
raise e
105109
except self._ignored_exceptions:
106110
return True
107111
time.sleep(self._poll)

py/test/selenium/webdriver/common/webdriverwait_tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import pytest
2121

22-
from selenium.common.exceptions import TimeoutException
22+
from selenium.common.exceptions import TimeoutException, InvalidSelectorException
2323
from selenium.common.exceptions import StaleElementReferenceException
2424
from selenium.common.exceptions import WebDriverException
2525
from selenium.common.exceptions import InvalidElementStateException
@@ -32,6 +32,12 @@ def throwSERE(driver):
3232
raise StaleElementReferenceException("test")
3333

3434

35+
def testShouldFailWithInvalidSelectorException(driver, pages):
36+
pages.load("dynamic.html")
37+
with pytest.raises(InvalidSelectorException):
38+
WebDriverWait(driver, 0.7).until(EC.presence_of_element_located((By.XPATH, "//*[contains(@id,'something'")))
39+
40+
3541
def testShouldExplicitlyWaitForASingleElement(driver, pages):
3642
pages.load("dynamic.html")
3743
add = driver.find_element(By.ID, "adder")

0 commit comments

Comments
 (0)