exceptions.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """
  18. Exceptions that may happen in all the webdriver code.
  19. """
  20. class WebDriverException(Exception):
  21. """
  22. Base webdriver exception.
  23. """
  24. def __init__(self, msg=None, screen=None, stacktrace=None):
  25. self.msg = msg
  26. self.screen = screen
  27. self.stacktrace = stacktrace
  28. def __str__(self):
  29. exception_msg = "Message: %s\n" % self.msg
  30. if self.screen is not None:
  31. exception_msg += "Screenshot: available via screen\n"
  32. if self.stacktrace is not None:
  33. stacktrace = "\n".join(self.stacktrace)
  34. exception_msg += "Stacktrace:\n%s" % stacktrace
  35. return exception_msg
  36. class ErrorInResponseException(WebDriverException):
  37. """
  38. Thrown when an error has occurred on the server side.
  39. This may happen when communicating with the firefox extension
  40. or the remote driver server.
  41. """
  42. def __init__(self, response, msg):
  43. WebDriverException.__init__(self, msg)
  44. self.response = response
  45. class InvalidSwitchToTargetException(WebDriverException):
  46. """
  47. Thrown when frame or window target to be switched doesn't exist.
  48. """
  49. pass
  50. class NoSuchFrameException(InvalidSwitchToTargetException):
  51. """
  52. Thrown when frame target to be switched doesn't exist.
  53. """
  54. pass
  55. class NoSuchWindowException(InvalidSwitchToTargetException):
  56. """
  57. Thrown when window target to be switched doesn't exist.
  58. To find the current set of active window handles, you can get a list
  59. of the active window handles in the following way::
  60. print driver.window_handles
  61. """
  62. pass
  63. class NoSuchElementException(WebDriverException):
  64. """
  65. Thrown when element could not be found.
  66. If you encounter this exception, you may want to check the following:
  67. * Check your selector used in your find_by...
  68. * Element may not yet be on the screen at the time of the find operation,
  69. (webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait()
  70. for how to write a wait wrapper to wait for an element to appear.
  71. """
  72. pass
  73. class NoSuchAttributeException(WebDriverException):
  74. """
  75. Thrown when the attribute of element could not be found.
  76. You may want to check if the attribute exists in the particular browser you are
  77. testing against. Some browsers may have different property names for the same
  78. property. (IE8's .innerText vs. Firefox .textContent)
  79. """
  80. pass
  81. class StaleElementReferenceException(WebDriverException):
  82. """
  83. Thrown when a reference to an element is now "stale".
  84. Stale means the element no longer appears on the DOM of the page.
  85. Possible causes of StaleElementReferenceException include, but not limited to:
  86. * You are no longer on the same page, or the page may have refreshed since the element
  87. was located.
  88. * The element may have been removed and re-added to the screen, since it was located.
  89. Such as an element being relocated.
  90. This can happen typically with a javascript framework when values are updated and the
  91. node is rebuilt.
  92. * Element may have been inside an iframe or another context which was refreshed.
  93. """
  94. pass
  95. class InvalidElementStateException(WebDriverException):
  96. """
  97. """
  98. pass
  99. class UnexpectedAlertPresentException(WebDriverException):
  100. """
  101. Thrown when an unexpected alert is appeared.
  102. Usually raised when when an expected modal is blocking webdriver form executing any
  103. more commands.
  104. """
  105. def __init__(self, msg=None, screen=None, stacktrace=None, alert_text=None):
  106. super(UnexpectedAlertPresentException, self).__init__(msg, screen, stacktrace)
  107. self.alert_text = alert_text
  108. def __str__(self):
  109. return "Alert Text: %s\n%s" % (self.alert_text, super(UnexpectedAlertPresentException, self).__str__())
  110. class NoAlertPresentException(WebDriverException):
  111. """
  112. Thrown when switching to no presented alert.
  113. This can be caused by calling an operation on the Alert() class when an alert is
  114. not yet on the screen.
  115. """
  116. pass
  117. class ElementNotVisibleException(InvalidElementStateException):
  118. """
  119. Thrown when an element is present on the DOM, but
  120. it is not visible, and so is not able to be interacted with.
  121. Most commonly encountered when trying to click or read text
  122. of an element that is hidden from view.
  123. """
  124. pass
  125. class ElementNotInteractableException(InvalidElementStateException):
  126. """
  127. Thrown when an element is present in the DOM but interactions
  128. with that element will hit another element do to paint order
  129. """
  130. pass
  131. class ElementNotSelectableException(InvalidElementStateException):
  132. """
  133. Thrown when trying to select an unselectable element.
  134. For example, selecting a 'script' element.
  135. """
  136. pass
  137. class InvalidCookieDomainException(WebDriverException):
  138. """
  139. Thrown when attempting to add a cookie under a different domain
  140. than the current URL.
  141. """
  142. pass
  143. class UnableToSetCookieException(WebDriverException):
  144. """
  145. Thrown when a driver fails to set a cookie.
  146. """
  147. pass
  148. class RemoteDriverServerException(WebDriverException):
  149. """
  150. """
  151. pass
  152. class TimeoutException(WebDriverException):
  153. """
  154. Thrown when a command does not complete in enough time.
  155. """
  156. pass
  157. class MoveTargetOutOfBoundsException(WebDriverException):
  158. """
  159. Thrown when the target provided to the `ActionsChains` move()
  160. method is invalid, i.e. out of document.
  161. """
  162. pass
  163. class UnexpectedTagNameException(WebDriverException):
  164. """
  165. Thrown when a support class did not get an expected web element.
  166. """
  167. pass
  168. class InvalidSelectorException(NoSuchElementException):
  169. """
  170. Thrown when the selector which is used to find an element does not return
  171. a WebElement. Currently this only happens when the selector is an xpath
  172. expression and it is either syntactically invalid (i.e. it is not a
  173. xpath expression) or the expression does not select WebElements
  174. (e.g. "count(//input)").
  175. """
  176. pass
  177. class ImeNotAvailableException(WebDriverException):
  178. """
  179. Thrown when IME support is not available. This exception is thrown for every IME-related
  180. method call if IME support is not available on the machine.
  181. """
  182. pass
  183. class ImeActivationFailedException(WebDriverException):
  184. """
  185. Thrown when activating an IME engine has failed.
  186. """
  187. pass
  188. class InvalidArgumentException(WebDriverException):
  189. """
  190. The arguments passed to a command are either invalid or malformed.
  191. """
  192. pass