Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/remote/switch_to.py
4011 views
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
19
from selenium.common.exceptions import NoSuchElementException, NoSuchFrameException, NoSuchWindowException
20
from selenium.webdriver.common.alert import Alert
21
from selenium.webdriver.common.by import By
22
from selenium.webdriver.remote.command import Command
23
from selenium.webdriver.remote.webelement import WebElement
24
25
26
class SwitchTo:
27
def __init__(self, driver) -> None:
28
import weakref
29
30
self._driver = weakref.proxy(driver)
31
32
@property
33
def active_element(self) -> WebElement:
34
"""Returns the element with focus, or BODY if nothing has focus.
35
36
Example:
37
element = driver.switch_to.active_element
38
"""
39
return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)["value"]
40
41
@property
42
def alert(self) -> Alert:
43
"""Switches focus to an alert on the page.
44
45
Example:
46
alert = driver.switch_to.alert
47
"""
48
alert = Alert(self._driver)
49
_ = alert.text
50
return alert
51
52
def default_content(self) -> None:
53
"""Switch focus to the default frame.
54
55
Example:
56
driver.switch_to.default_content()
57
"""
58
self._driver.execute(Command.SWITCH_TO_FRAME, {"id": None})
59
60
def frame(self, frame_reference: str | int | WebElement) -> None:
61
"""Switch focus to the specified frame by index, name, or element.
62
63
Args:
64
frame_reference: The name of the frame to switch to, an integer representing the index,
65
or a WebElement that is an (i)frame to switch to.
66
67
Example:
68
driver.switch_to.frame("frame_name")
69
driver.switch_to.frame(1)
70
driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0])
71
"""
72
if isinstance(frame_reference, str):
73
try:
74
frame_reference = self._driver.find_element(By.ID, frame_reference)
75
except NoSuchElementException:
76
try:
77
frame_reference = self._driver.find_element(By.NAME, frame_reference)
78
except NoSuchElementException as exc:
79
raise NoSuchFrameException(frame_reference) from exc
80
81
self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference})
82
83
def new_window(self, type_hint: str | None = None) -> None:
84
"""Switches to a new top-level browsing context.
85
86
The type hint can be one of "tab" or "window". If not specified the
87
browser will automatically select it.
88
89
Example:
90
driver.switch_to.new_window("tab")
91
"""
92
value = self._driver.execute(Command.NEW_WINDOW, {"type": type_hint})["value"]
93
self._w3c_window(value["handle"])
94
95
def parent_frame(self) -> None:
96
"""Switch focus to the parent browsing context.
97
98
If the current context is already the top level browsing context, it remains unchanged.
99
100
Example:
101
driver.switch_to.parent_frame()
102
"""
103
self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)
104
105
def window(self, window_name: str) -> None:
106
"""Switches focus to the specified window.
107
108
Args:
109
window_name: The name or window handle of the window to switch to.
110
111
Example:
112
driver.switch_to.window("main")
113
"""
114
self._w3c_window(window_name)
115
116
def _w3c_window(self, window_name: str) -> None:
117
def send_handle(h):
118
self._driver.execute(Command.SWITCH_TO_WINDOW, {"handle": h})
119
120
try:
121
# Try using it as a handle first.
122
send_handle(window_name)
123
except NoSuchWindowException:
124
# Check every window to try to find the given window name.
125
original_handle = self._driver.current_window_handle
126
handles = self._driver.window_handles
127
for handle in handles:
128
send_handle(handle)
129
current_name = self._driver.execute_script("return window.name")
130
if window_name == current_name:
131
return
132
send_handle(original_handle)
133
raise
134
135