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