Skip to content

Commit 7477ac9

Browse files
added code and content for frames (#1915)[deploy site]
Co-authored-by: Sri Harsha <[email protected]>
1 parent 211a646 commit 7477ac9

File tree

5 files changed

+115
-106
lines changed

5 files changed

+115
-106
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,74 @@
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+
118
package dev.selenium.interactions;
219

3-
import dev.selenium.BaseTest;
20+
import org.junit.jupiter.api.Test;
21+
import org.openqa.selenium.*;
22+
import org.openqa.selenium.chrome.ChromeDriver;
23+
import java.time.Duration;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
26+
public class FramesTest{
427

5-
public class FramesTest extends BaseTest {
28+
@Test
29+
public void informationWithElements() {
30+
31+
WebDriver driver = new ChromeDriver();
32+
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
33+
34+
// Navigate to Url
35+
driver.get("https://www.selenium.dev/selenium/web/iframes.html");
36+
37+
38+
//switch To IFrame using Web Element
39+
WebElement iframe = driver.findElement(By.id("iframe1"));
40+
//Switch to the frame
41+
driver.switchTo().frame(iframe);
42+
assertEquals(true, driver.getPageSource().contains("We Leave From Here"));
43+
//Now we can type text into email field
44+
WebElement emailE= driver.findElement(By.id("email"));
45+
emailE.sendKeys("[email protected]");
46+
emailE.clear();
47+
driver.switchTo().defaultContent();
48+
49+
50+
//switch To IFrame using name or id
51+
driver.findElement(By.name("iframe1-name"));
52+
//Switch to the frame
53+
driver.switchTo().frame(iframe);
54+
assertEquals(true, driver.getPageSource().contains("We Leave From Here"));
55+
WebElement email=driver.findElement(By.id("email"));
56+
//Now we can type text into email field
57+
email.sendKeys("[email protected]");
58+
email.clear();
59+
driver.switchTo().defaultContent();
60+
61+
62+
//switch To IFrame using index
63+
driver.switchTo().frame(0);
64+
assertEquals(true, driver.getPageSource().contains("We Leave From Here"));
65+
66+
//leave frame
67+
driver.switchTo().defaultContent();
68+
assertEquals(true, driver.getPageSource().contains("This page has iframes"));
69+
70+
//quit the browser
71+
driver.quit();
72+
}
673

7-
}
74+
}

Diff for: website_and_docs/content/documentation/webdriver/interactions/frames.en.md

+15-27
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ However, if there are no buttons outside of the iframe, you might
6161
instead get a _no such element_ error. This happens because Selenium is
6262
only aware of the elements in the top level document. To interact with
6363
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.
6667

6768
## Using a WebElement
6869

@@ -71,16 +72,9 @@ find the frame using your preferred selector and switch to it.
7172

7273
{{< tabpane langEqualsHeader=true >}}
7374
{{< 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 >}}
8478
{{< tab header="Python" >}}
8579
# Store iframe web element
8680
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
@@ -140,15 +134,8 @@ one found will be switched to.
140134

141135
{{< tabpane langEqualsHeader=true >}}
142136
{{< 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" >}}
152139
{{< /tab >}}
153140
{{< tab header="Python" >}}
154141
# Switch frame by id
@@ -203,10 +190,11 @@ queried using _window.frames_ in JavaScript.
203190

204191
{{< tabpane langEqualsHeader=true >}}
205192
{{< 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" >}}
209196
{{< /tab >}}
197+
210198
{{< tab header="Ruby" >}}
211199
# Switch to the second frame
212200
driver.switch_to.frame(1)
@@ -240,10 +228,10 @@ like so:
240228

241229
{{< tabpane langEqualsHeader=true >}}
242230
{{< 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" >}}
246233
{{< /tab >}}
234+
247235
{{< tab header="Python" >}}
248236
# switch back to default content
249237
driver.switch_to.default_content()

Diff for: website_and_docs/content/documentation/webdriver/interactions/frames.ja.md

+9-25
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,9 @@ WebDriverは、Frameに切り替える3つの方法を提供します。
6161
WebElementを使用した切り替えは、最も柔軟なオプションです。好みのセレクタを使用してFrameを見つけ、それに切り替えることができます。
6262

6363
{{< tabpane langEqualsHeader=true >}}
64-
{{< tab header="Java" >}}
65-
//Store the web element
66-
WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe"));
67-
68-
//Switch to the frame
69-
driver.switchTo().frame(iframe);
70-
71-
//Now we can click the button
72-
driver.findElement(By.tagName("button")).click();
73-
{{< /tab >}}
64+
{{< tab header="Java" text=true >}}
65+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}}
66+
{{< /tab >}}
7467
{{< tab header="Python" >}}
7568
# Store iframe web element
7669
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
@@ -129,15 +122,8 @@ FrameまたはiFrameにidまたはname属性がある場合、代わりにこれ
129122
名前またはIDがページ上で一意でない場合、最初に見つかったものに切り替えます。
130123

131124
{{< tabpane langEqualsHeader=true >}}
132-
{{< tab header="Java" >}}
133-
//Using the ID
134-
driver.switchTo().frame("buttonframe");
135-
136-
//Or using the name instead
137-
driver.switchTo().frame("myframe");
138-
139-
//Now we can click the button
140-
driver.findElement(By.tagName("button")).click();
125+
{{< tab header="Java" text=true >}}
126+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}}
141127
{{< /tab >}}
142128
{{< tab header="Python" >}}
143129
# Switch frame by id
@@ -190,9 +176,8 @@ driver.findElement(By.tagName("button")).click()
190176
JavaScriptの _window.frames_ を使用して照会できるように、Frameのインデックスを使用することもできます。
191177

192178
{{< tabpane langEqualsHeader=true >}}
193-
{{< tab header="Java" >}}
194-
// Switches to the second frame
195-
driver.switchTo().frame(1);
179+
{{< tab header="Java" text=true >}}
180+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}}
196181
{{< /tab >}}
197182
{{< tab header="Ruby" >}}
198183
# Switch to the second frame
@@ -225,9 +210,8 @@ driver.switchTo().frame(1)
225210
iFrameまたはFrameセットを終了するには、次のようにデフォルトのコンテンツに切り替えます。
226211

227212
{{< tabpane langEqualsHeader=true >}}
228-
{{< tab header="Java" >}}
229-
// Return to the top level
230-
driver.switchTo().defaultContent();
213+
{{< tab header="Java" text=true >}}
214+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}}
231215
{{< /tab >}}
232216
{{< tab header="Python" >}}
233217
# switch back to default content

Diff for: website_and_docs/content/documentation/webdriver/interactions/frames.pt-br.md

+9-25
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,9 @@ Alternar usando um WebElement é a opção mais flexível. Você pode
6969
encontrar o quadro usando seu seletor preferido e mudar para ele.
7070

7171
{{< tabpane langEqualsHeader=true >}}
72-
{{< tab header="Java" >}}
73-
//Store the web element
74-
WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe"));
75-
76-
//Switch to the frame
77-
driver.switchTo().frame(iframe);
78-
79-
//Now we can click the button
80-
driver.findElement(By.tagName("button")).click();
81-
{{< /tab >}}
72+
{{< tab header="Java" text=true >}}
73+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}}
74+
{{< /tab >}}
8275
{{< tab header="Python" >}}
8376
# Store iframe web element
8477
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
@@ -137,15 +130,8 @@ usado alternativamente. Se o name ou ID não for exclusivo na página, o
137130
primeiro encontrado será utilizado.
138131

139132
{{< tabpane langEqualsHeader=true >}}
140-
{{< tab header="Java" >}}
141-
//Using the ID
142-
driver.switchTo().frame("buttonframe");
143-
144-
//Or using the name instead
145-
driver.switchTo().frame("myframe");
146-
147-
//Now we can click the button
148-
driver.findElement(By.tagName("button")).click();
133+
{{< tab header="Java" text=true >}}
134+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}}
149135
{{< /tab >}}
150136
{{< tab header="Python" >}}
151137
# Switch frame by id
@@ -199,9 +185,8 @@ Também é possível usar o índice do frame, podendo ser
199185
consultado usando _window.frames_ em JavaScript.
200186

201187
{{< tabpane langEqualsHeader=true >}}
202-
{{< tab header="Java" >}}
203-
// Switches to the second frame
204-
driver.switchTo().frame(1);
188+
{{< tab header="Java" text=true >}}
189+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}}
205190
{{< /tab >}}
206191
{{< tab header="Ruby" >}}
207192
# Switch to the second frame
@@ -235,9 +220,8 @@ Para deixar um iframe ou frameset, volte para o conteúdo padrão
235220
como a seguir:
236221

237222
{{< tabpane langEqualsHeader=true >}}
238-
{{< tab header="Java" >}}
239-
// Return to the top level
240-
driver.switchTo().defaultContent();
223+
{{< tab header="Java" text=true >}}
224+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}}
241225
{{< /tab >}}
242226
{{< tab header="Python" >}}
243227
# switch back to default content

Diff for: website_and_docs/content/documentation/webdriver/interactions/frames.zh-cn.md

+12-26
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,11 @@ driver.findElement(By.tagName("button")).click()
6060
使用 WebElement 进行切换是最灵活的选择。您可以使用首选的选择器找到框架并切换到它。
6161

6262
{{< tabpane langEqualsHeader=true >}}
63-
{{< tab header="Java" >}}
64-
// 存储网页元素
65-
WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe"));
6663

67-
// 切换到 frame
68-
driver.switchTo().frame(iframe);
64+
{{< tab header="Java" text=true >}}
65+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}}
66+
{{< /tab >}}
6967

70-
// 现在可以点击按钮
71-
driver.findElement(By.tagName("button")).click();
72-
{{< /tab >}}
7368
{{< tab header="Python" >}}
7469
# 存储网页元素
7570
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
@@ -128,16 +123,9 @@ driver.findElement(By.tagName("button")).click()
128123
那么将切换到找到的第一个。
129124

130125
{{< tabpane langEqualsHeader=true >}}
131-
{{< tab header="Java" >}}
132-
// 使用 ID
133-
driver.switchTo().frame("buttonframe");
134-
135-
// 或者使用 name 代替
136-
driver.switchTo().frame("myframe");
137-
138-
// 现在可以点击按钮
139-
driver.findElement(By.tagName("button")).click();
140-
{{< /tab >}}
126+
{{< tab header="Java" text=true >}}
127+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}}
128+
{{< /tab >}}
141129
{{< tab header="Python" >}}
142130
# 通过 id 切换框架
143131
driver.switch_to.frame('buttonframe')
@@ -191,10 +179,9 @@ driver.findElement(By.tagName("button")).click()
191179
_window.frames_ 进行查询.
192180

193181
{{< tabpane langEqualsHeader=true >}}
194-
{{< tab header="Java" >}}
195-
// 切换到第 2 个框架
196-
driver.switchTo().frame(1);
197-
{{< /tab >}}
182+
{{< tab header="Java" text=true >}}
183+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}}
184+
{{< /tab >}}
198185
{{< tab header="Ruby" >}}
199186
# 切换到第 2 个框架
200187
driver.switch_to.frame(1)
@@ -226,10 +213,9 @@ driver.switchTo().frame(1)
226213
离开 iframe 或 frameset,切换回默认内容,如下所示:
227214

228215
{{< tabpane langEqualsHeader=true >}}
229-
{{< tab header="Java" >}}
230-
// 回到顶层
231-
driver.switchTo().defaultContent();
232-
{{< /tab >}}
216+
{{< tab header="Java" text=true >}}
217+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}}
218+
{{< /tab >}}
233219
{{< tab header="Python" >}}
234220
# 切回到默认内容
235221
driver.switch_to.default_content()

0 commit comments

Comments
 (0)