diff --git a/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java b/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java index bf92911e3642..be57fcf589b8 100644 --- a/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java +++ b/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java @@ -1,7 +1,74 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + package dev.selenium.interactions; -import dev.selenium.BaseTest; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.*; +import org.openqa.selenium.chrome.ChromeDriver; +import java.time.Duration; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FramesTest{ -public class FramesTest extends BaseTest { + @Test + public void informationWithElements() { + + WebDriver driver = new ChromeDriver(); + driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); + + // Navigate to Url + driver.get("https://www.selenium.dev/selenium/web/iframes.html"); + + + //switch To IFrame using Web Element + WebElement iframe = driver.findElement(By.id("iframe1")); + //Switch to the frame + driver.switchTo().frame(iframe); + assertEquals(true, driver.getPageSource().contains("We Leave From Here")); + //Now we can type text into email field + WebElement emailE= driver.findElement(By.id("email")); + emailE.sendKeys("admin@selenium.dev"); + emailE.clear(); + driver.switchTo().defaultContent(); + + + //switch To IFrame using name or id + driver.findElement(By.name("iframe1-name")); + //Switch to the frame + driver.switchTo().frame(iframe); + assertEquals(true, driver.getPageSource().contains("We Leave From Here")); + WebElement email=driver.findElement(By.id("email")); + //Now we can type text into email field + email.sendKeys("admin@selenium.dev"); + email.clear(); + driver.switchTo().defaultContent(); + + + //switch To IFrame using index + driver.switchTo().frame(0); + assertEquals(true, driver.getPageSource().contains("We Leave From Here")); + + //leave frame + driver.switchTo().defaultContent(); + assertEquals(true, driver.getPageSource().contains("This page has iframes")); + + //quit the browser + driver.quit(); + } -} +} \ No newline at end of file diff --git a/website_and_docs/content/documentation/webdriver/interactions/frames.en.md b/website_and_docs/content/documentation/webdriver/interactions/frames.en.md index 1ebae425bee8..6900c6b2de14 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/frames.en.md +++ b/website_and_docs/content/documentation/webdriver/interactions/frames.en.md @@ -61,8 +61,9 @@ However, if there are no buttons outside of the iframe, you might instead get a _no such element_ error. This happens because Selenium is only aware of the elements in the top level document. To interact with the button, we will need to first switch to the frame, in a similar way -to how we switch windows. WebDriver offers three ways of switching to -a frame. +to how we switch windows. +WebDriver offers three ways of switching to a frame. Following example code +shows how we can do that, using a live web example. ## Using a WebElement @@ -71,16 +72,9 @@ find the frame using your preferred selector and switch to it. {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -//Store the web element -WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe")); - -//Switch to the frame -driver.switchTo().frame(iframe); - -//Now we can click the button -driver.findElement(By.tagName("button")).click(); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}} +{{< /tab >}} {{< tab header="Python" >}} # Store iframe web element iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe") @@ -140,15 +134,8 @@ one found will be switched to. {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -//Using the ID -driver.switchTo().frame("buttonframe"); - -//Or using the name instead -driver.switchTo().frame("myframe"); - -//Now we can click the button -driver.findElement(By.tagName("button")).click(); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}} {{< /tab >}} {{< tab header="Python" >}} # Switch frame by id @@ -203,10 +190,11 @@ queried using _window.frames_ in JavaScript. {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -// Switches to the second frame -driver.switchTo().frame(1); + + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}} {{< /tab >}} + {{< tab header="Ruby" >}} # Switch to the second frame driver.switch_to.frame(1) @@ -240,10 +228,10 @@ like so: {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -// Return to the top level -driver.switchTo().defaultContent(); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}} {{< /tab >}} + {{< tab header="Python" >}} # switch back to default content driver.switch_to.default_content() diff --git a/website_and_docs/content/documentation/webdriver/interactions/frames.ja.md b/website_and_docs/content/documentation/webdriver/interactions/frames.ja.md index 43408d5f6979..0c02e8d41b90 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/frames.ja.md +++ b/website_and_docs/content/documentation/webdriver/interactions/frames.ja.md @@ -61,16 +61,9 @@ WebDriverは、Frameに切り替える3つの方法を提供します。 WebElementを使用した切り替えは、最も柔軟なオプションです。好みのセレクタを使用してFrameを見つけ、それに切り替えることができます。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Store the web element -WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe")); - -//Switch to the frame -driver.switchTo().frame(iframe); - -//Now we can click the button -driver.findElement(By.tagName("button")).click(); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}} +{{< /tab >}} {{< tab header="Python" >}} # Store iframe web element iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe") @@ -129,15 +122,8 @@ FrameまたはiFrameにidまたはname属性がある場合、代わりにこれ 名前またはIDがページ上で一意でない場合、最初に見つかったものに切り替えます。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Using the ID -driver.switchTo().frame("buttonframe"); - -//Or using the name instead -driver.switchTo().frame("myframe"); - -//Now we can click the button -driver.findElement(By.tagName("button")).click(); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}} {{< /tab >}} {{< tab header="Python" >}} # Switch frame by id @@ -190,9 +176,8 @@ driver.findElement(By.tagName("button")).click() JavaScriptの _window.frames_ を使用して照会できるように、Frameのインデックスを使用することもできます。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -// Switches to the second frame -driver.switchTo().frame(1); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}} {{< /tab >}} {{< tab header="Ruby" >}} # Switch to the second frame @@ -225,9 +210,8 @@ driver.switchTo().frame(1) iFrameまたはFrameセットを終了するには、次のようにデフォルトのコンテンツに切り替えます。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -// Return to the top level -driver.switchTo().defaultContent(); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}} {{< /tab >}} {{< tab header="Python" >}} # switch back to default content diff --git a/website_and_docs/content/documentation/webdriver/interactions/frames.pt-br.md b/website_and_docs/content/documentation/webdriver/interactions/frames.pt-br.md index 7a2c0025c3fa..6effc3ad22fa 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/frames.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/interactions/frames.pt-br.md @@ -69,16 +69,9 @@ Alternar usando um WebElement é a opção mais flexível. Você pode encontrar o quadro usando seu seletor preferido e mudar para ele. {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Store the web element -WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe")); - -//Switch to the frame -driver.switchTo().frame(iframe); - -//Now we can click the button -driver.findElement(By.tagName("button")).click(); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}} +{{< /tab >}} {{< tab header="Python" >}} # Store iframe web element 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 primeiro encontrado será utilizado. {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Using the ID -driver.switchTo().frame("buttonframe"); - -//Or using the name instead -driver.switchTo().frame("myframe"); - -//Now we can click the button -driver.findElement(By.tagName("button")).click(); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}} {{< /tab >}} {{< tab header="Python" >}} # Switch frame by id @@ -199,9 +185,8 @@ Também é possível usar o índice do frame, podendo ser consultado usando _window.frames_ em JavaScript. {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -// Switches to the second frame -driver.switchTo().frame(1); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}} {{< /tab >}} {{< tab header="Ruby" >}} # Switch to the second frame @@ -235,9 +220,8 @@ Para deixar um iframe ou frameset, volte para o conteúdo padrão como a seguir: {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -// Return to the top level -driver.switchTo().defaultContent(); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}} {{< /tab >}} {{< tab header="Python" >}} # switch back to default content diff --git a/website_and_docs/content/documentation/webdriver/interactions/frames.zh-cn.md b/website_and_docs/content/documentation/webdriver/interactions/frames.zh-cn.md index fe5028c720ef..4f6696307018 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/frames.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/interactions/frames.zh-cn.md @@ -60,16 +60,11 @@ driver.findElement(By.tagName("button")).click() 使用 WebElement 进行切换是最灵活的选择。您可以使用首选的选择器找到框架并切换到它。 {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -// 存储网页元素 -WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe")); -// 切换到 frame -driver.switchTo().frame(iframe); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L38-L46" >}} + {{< /tab >}} -// 现在可以点击按钮 -driver.findElement(By.tagName("button")).click(); -{{< /tab >}} {{< tab header="Python" >}} # 存储网页元素 iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe") @@ -128,16 +123,9 @@ driver.findElement(By.tagName("button")).click() 那么将切换到找到的第一个。 {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -// 使用 ID -driver.switchTo().frame("buttonframe"); - -// 或者使用 name 代替 -driver.switchTo().frame("myframe"); - -// 现在可以点击按钮 -driver.findElement(By.tagName("button")).click(); -{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L50-L58" >}} + {{< /tab >}} {{< tab header="Python" >}} # 通过 id 切换框架 driver.switch_to.frame('buttonframe') @@ -191,10 +179,9 @@ driver.findElement(By.tagName("button")).click() _window.frames_ 进行查询. {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -// 切换到第 2 个框架 -driver.switchTo().frame(1); -{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L62-L63" >}} + {{< /tab >}} {{< tab header="Ruby" >}} # 切换到第 2 个框架 driver.switch_to.frame(1) @@ -226,10 +213,9 @@ driver.switchTo().frame(1) 离开 iframe 或 frameset,切换回默认内容,如下所示: {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -// 回到顶层 -driver.switchTo().defaultContent(); -{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/FramesTest.java#L66-L67" >}} + {{< /tab >}} {{< tab header="Python" >}} # 切回到默认内容 driver.switch_to.default_content()