Path: blob/trunk/py/selenium/webdriver/remote/switch_to.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from typing import Optional, Union1819from selenium.common.exceptions import NoSuchElementException, NoSuchFrameException, NoSuchWindowException20from selenium.webdriver.common.alert import Alert21from selenium.webdriver.common.by import By22from selenium.webdriver.remote.webelement import WebElement2324from .command import Command252627class SwitchTo:28def __init__(self, driver) -> None:29import weakref3031self._driver = weakref.proxy(driver)3233@property34def active_element(self) -> WebElement:35"""Returns the element with focus, or BODY if nothing has focus.3637:Usage:38::3940element = driver.switch_to.active_element41"""42return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)["value"]4344@property45def alert(self) -> Alert:46"""Switches focus to an alert on the page.4748:Usage:49::5051alert = driver.switch_to.alert52"""53alert = Alert(self._driver)54_ = alert.text55return alert5657def default_content(self) -> None:58"""Switch focus to the default frame.5960:Usage:61::6263driver.switch_to.default_content()64"""65self._driver.execute(Command.SWITCH_TO_FRAME, {"id": None})6667def frame(self, frame_reference: Union[str, int, WebElement]) -> None:68"""Switches focus to the specified frame, by index, name, or69webelement.7071:Args:72- frame_reference: The name of the window to switch to, an integer representing the index,73or a webelement that is an (i)frame to switch to.7475:Usage:76::7778driver.switch_to.frame("frame_name")79driver.switch_to.frame(1)80driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0])81"""82if isinstance(frame_reference, str):83try:84frame_reference = self._driver.find_element(By.ID, frame_reference)85except NoSuchElementException:86try:87frame_reference = self._driver.find_element(By.NAME, frame_reference)88except NoSuchElementException as exc:89raise NoSuchFrameException(frame_reference) from exc9091self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference})9293def new_window(self, type_hint: Optional[str] = None) -> None:94"""Switches to a new top-level browsing context.9596The type hint can be one of "tab" or "window". If not specified the97browser will automatically select it.9899:Usage:100::101102driver.switch_to.new_window("tab")103"""104value = self._driver.execute(Command.NEW_WINDOW, {"type": type_hint})["value"]105self._w3c_window(value["handle"])106107def parent_frame(self) -> None:108"""Switches focus to the parent context. If the current context is the109top level browsing context, the context remains unchanged.110111:Usage:112::113114driver.switch_to.parent_frame()115"""116self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)117118def window(self, window_name: str) -> None:119"""Switches focus to the specified window.120121:Args:122- window_name: The name or window handle of the window to switch to.123124:Usage:125::126127driver.switch_to.window("main")128"""129self._w3c_window(window_name)130131def _w3c_window(self, window_name: str) -> None:132def send_handle(h):133self._driver.execute(Command.SWITCH_TO_WINDOW, {"handle": h})134135try:136# Try using it as a handle first.137send_handle(window_name)138except NoSuchWindowException:139# Check every window to try to find the given window name.140original_handle = self._driver.current_window_handle141handles = self._driver.window_handles142for handle in handles:143send_handle(handle)144current_name = self._driver.execute_script("return window.name")145if window_name == current_name:146return147send_handle(original_handle)148raise149150151