Path: blob/trunk/py/selenium/webdriver/remote/switch_to.py
4011 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.161718from selenium.common.exceptions import NoSuchElementException, NoSuchFrameException, NoSuchWindowException19from selenium.webdriver.common.alert import Alert20from selenium.webdriver.common.by import By21from selenium.webdriver.remote.command import Command22from selenium.webdriver.remote.webelement import WebElement232425class SwitchTo:26def __init__(self, driver) -> None:27import weakref2829self._driver = weakref.proxy(driver)3031@property32def active_element(self) -> WebElement:33"""Returns the element with focus, or BODY if nothing has focus.3435Example:36element = driver.switch_to.active_element37"""38return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)["value"]3940@property41def alert(self) -> Alert:42"""Switches focus to an alert on the page.4344Example:45alert = driver.switch_to.alert46"""47alert = Alert(self._driver)48_ = alert.text49return alert5051def default_content(self) -> None:52"""Switch focus to the default frame.5354Example:55driver.switch_to.default_content()56"""57self._driver.execute(Command.SWITCH_TO_FRAME, {"id": None})5859def frame(self, frame_reference: str | int | WebElement) -> None:60"""Switch focus to the specified frame by index, name, or element.6162Args:63frame_reference: The name of the frame to switch to, an integer representing the index,64or a WebElement that is an (i)frame to switch to.6566Example:67driver.switch_to.frame("frame_name")68driver.switch_to.frame(1)69driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0])70"""71if isinstance(frame_reference, str):72try:73frame_reference = self._driver.find_element(By.ID, frame_reference)74except NoSuchElementException:75try:76frame_reference = self._driver.find_element(By.NAME, frame_reference)77except NoSuchElementException as exc:78raise NoSuchFrameException(frame_reference) from exc7980self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference})8182def new_window(self, type_hint: str | None = None) -> None:83"""Switches to a new top-level browsing context.8485The type hint can be one of "tab" or "window". If not specified the86browser will automatically select it.8788Example:89driver.switch_to.new_window("tab")90"""91value = self._driver.execute(Command.NEW_WINDOW, {"type": type_hint})["value"]92self._w3c_window(value["handle"])9394def parent_frame(self) -> None:95"""Switch focus to the parent browsing context.9697If the current context is already the top level browsing context, it remains unchanged.9899Example:100driver.switch_to.parent_frame()101"""102self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)103104def window(self, window_name: str) -> None:105"""Switches focus to the specified window.106107Args:108window_name: The name or window handle of the window to switch to.109110Example:111driver.switch_to.window("main")112"""113self._w3c_window(window_name)114115def _w3c_window(self, window_name: str) -> None:116def send_handle(h):117self._driver.execute(Command.SWITCH_TO_WINDOW, {"handle": h})118119try:120# Try using it as a handle first.121send_handle(window_name)122except NoSuchWindowException:123# Check every window to try to find the given window name.124original_handle = self._driver.current_window_handle125handles = self._driver.window_handles126for handle in handles:127send_handle(handle)128current_name = self._driver.execute_script("return window.name")129if window_name == current_name:130return131send_handle(original_handle)132raise133134135