switch_to.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. from .command import Command
  18. from selenium.webdriver.common.alert import Alert
  19. from selenium.webdriver.common.by import By
  20. from selenium.common.exceptions import NoSuchElementException, NoSuchFrameException
  21. try:
  22. basestring
  23. except NameError:
  24. basestring = str
  25. class SwitchTo:
  26. def __init__(self, driver):
  27. self._driver = driver
  28. @property
  29. def active_element(self):
  30. """
  31. Returns the element with focus, or BODY if nothing has focus.
  32. :Usage:
  33. element = driver.switch_to.active_element
  34. """
  35. if self._driver.w3c:
  36. return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)
  37. else:
  38. return self._driver.execute(Command.GET_ACTIVE_ELEMENT)['value']
  39. @property
  40. def alert(self):
  41. """
  42. Switches focus to an alert on the page.
  43. :Usage:
  44. alert = driver.switch_to.alert
  45. """
  46. return Alert(self._driver)
  47. def default_content(self):
  48. """
  49. Switch focus to the default frame.
  50. :Usage:
  51. driver.switch_to.default_content()
  52. """
  53. self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})
  54. def frame(self, frame_reference):
  55. """
  56. Switches focus to the specified frame, by index, name, or webelement.
  57. :Args:
  58. - frame_reference: The name of the window to switch to, an integer representing the index,
  59. or a webelement that is an (i)frame to switch to.
  60. :Usage:
  61. driver.switch_to.frame('frame_name')
  62. driver.switch_to.frame(1)
  63. driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
  64. """
  65. if isinstance(frame_reference, basestring) and self._driver.w3c:
  66. try:
  67. frame_reference = self._driver.find_element(By.ID, frame_reference)
  68. except NoSuchElementException:
  69. try:
  70. frame_reference = self._driver.find_element(By.NAME, frame_reference)
  71. except NoSuchElementException:
  72. raise NoSuchFrameException(frame_reference)
  73. self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
  74. def parent_frame(self):
  75. """
  76. Switches focus to the parent context. If the current context is the top
  77. level browsing context, the context remains unchanged.
  78. :Usage:
  79. driver.switch_to.parent_frame()
  80. """
  81. self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)
  82. def window(self, window_name):
  83. """
  84. Switches focus to the specified window.
  85. :Args:
  86. - window_name: The name or window handle of the window to switch to.
  87. :Usage:
  88. driver.switch_to.window('main')
  89. """
  90. data = {'name': window_name}
  91. if self._driver.w3c:
  92. data = {'handle': window_name}
  93. self._driver.execute(Command.SWITCH_TO_WINDOW, data)