@@ -61,8 +61,9 @@ However, if there are no buttons outside of the iframe, you might
61
61
instead get a _ no such element_ error. This happens because Selenium is
62
62
only aware of the elements in the top level document. To interact with
63
63
the button, we will need to first switch to the frame, in a similar way
64
- to how we switch windows. WebDriver offers three ways of switching to
65
- a frame.
64
+ to how we switch windows.
65
+ WebDriver offers three ways of switching to a frame. Following example code
66
+ shows how we can do that, using a live web example.
66
67
67
68
## Using a WebElement
68
69
@@ -71,16 +72,9 @@ find the frame using your preferred selector and switch to it.
71
72
72
73
{{< tabpane langEqualsHeader=true >}}
73
74
{{< badge-examples >}}
74
- {{< tab header="Java" >}}
75
- //Store the web element
76
- WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe"));
77
-
78
- //Switch to the frame
79
- driver.switchTo().frame(iframe);
80
-
81
- //Now we can click the button
82
- driver.findElement(By.tagName("button")).click();
83
- {{< /tab >}}
75
+ {{< tab header="Java" text=true >}}
76
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}}
77
+ {{< /tab >}}
84
78
{{< tab header="Python" >}}
85
79
# Store iframe web element
86
80
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
@@ -140,15 +134,8 @@ one found will be switched to.
140
134
141
135
{{< tabpane langEqualsHeader=true >}}
142
136
{{< badge-examples >}}
143
- {{< tab header="Java" >}}
144
- //Using the ID
145
- driver.switchTo().frame("buttonframe");
146
-
147
- //Or using the name instead
148
- driver.switchTo().frame("myframe");
149
-
150
- //Now we can click the button
151
- driver.findElement(By.tagName("button")).click();
137
+ {{< tab header="Java" text=true >}}
138
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}}
152
139
{{< /tab >}}
153
140
{{< tab header="Python" >}}
154
141
# Switch frame by id
@@ -203,10 +190,11 @@ queried using _window.frames_ in JavaScript.
203
190
204
191
{{< tabpane langEqualsHeader=true >}}
205
192
{{< badge-examples >}}
206
- {{< tab header="Java" >}}
207
- // Switches to the second frame
208
- driver.switchTo().frame(1);
193
+
194
+ {{< tab header="Java" text=true >}}
195
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}}
209
196
{{< /tab >}}
197
+
210
198
{{< tab header="Ruby" >}}
211
199
# Switch to the second frame
212
200
driver.switch_to.frame(1)
@@ -240,10 +228,10 @@ like so:
240
228
241
229
{{< tabpane langEqualsHeader=true >}}
242
230
{{< badge-examples >}}
243
- {{< tab header="Java" >}}
244
- // Return to the top level
245
- driver.switchTo().defaultContent();
231
+ {{< tab header="Java" text=true >}}
232
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}}
246
233
{{< /tab >}}
234
+
247
235
{{< tab header="Python" >}}
248
236
# switch back to default content
249
237
driver.switch_to.default_content()
0 commit comments