Working with IFrames and frames
Frames são um meio obsoleto de construir um layout de site a partir de vários documentos no mesmo domínio. É improvável que você trabalhe com eles a menos que você esteja trabalhando com um webapp pré-HTML5. Iframes permitem a inserção de um documento de um domínio totalmente diferente, e são ainda comumente usado.
Se você precisa trabalhar com frames ou iframes, o WebDriver permite que você trabalhe com eles da mesma maneira. Considere um botão dentro de um iframe. Se inspecionarmos o elemento usando as ferramentas de desenvolvimento do navegador, podemos ver o seguinte:
<div id="modal">
<iframe id="buttonframe" name="myframe" src="https://seleniumhq.github.io">
<button>Click here</button>
</iframe>
</div>
Se não fosse pelo iframe, esperaríamos clicar no botão usando algo como:
//This won't work
driver.findElement(By.tagName("button")).click();
# This Wont work
driver.find_element(By.TAG_NAME, 'button').click()
//This won't work
driver.FindElement(By.TagName("button")).Click();
# This won't work
driver.find_element(:tag_name,'button').click
// This won't work
await driver.findElement(By.css('button')).click();
//This won't work
driver.findElement(By.tagName("button")).click()
No entanto, se não houver botões fora do iframe, você pode em vez disso, obter um erro no such element. Isso acontece porque o Selenium é ciente apenas dos elementos no documento de nível superior. Para interagir com o botão, precisamos primeiro mudar para o quadro, de forma semelhante a como alternamos janelas. WebDriver oferece três maneiras de mudar para um frame.
Usando um WebElement
Alternar usando um WebElement é a opção mais flexível. Você pode encontrar o quadro usando seu seletor preferido e mudar para ele.
//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();/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java
// 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 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{
@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
WebElement iframe1=driver.findElement(By.name("iframe1-name"));
//Switch to the frame
driver.switchTo().frame(iframe1);
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();
}
}
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(By.ID, "iframe1")
driver.switch_to.frame(iframe)
assert "We Leave From Here" in driver.page_source
email_element = driver.find_element(By.ID, "email")
email_element.send_keys("admin@selenium.dev")
email_element.clear()
driver.switch_to.default_content()/examples/python/tests/interactions/test_frames.py
# 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.
from selenium import webdriver
from selenium.webdriver.common.by import By
#set chrome and launch web page
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/iframes.html")
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(By.ID, "iframe1")
driver.switch_to.frame(iframe)
assert "We Leave From Here" in driver.page_source
email_element = driver.find_element(By.ID, "email")
email_element.send_keys("admin@selenium.dev")
email_element.clear()
driver.switch_to.default_content()
# --- Switch to iframe using name or ID ---
iframe1=driver.find_element(By.NAME, "iframe1-name") # (This line doesn't switch, just locates)
driver.switch_to.frame(iframe1)
assert "We Leave From Here" in driver.page_source
email = driver.find_element(By.ID, "email")
email.send_keys("admin@selenium.dev")
email.clear()
driver.switch_to.default_content()
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
assert "We Leave From Here" in driver.page_source
# --- Final page content check ---
driver.switch_to.default_content()
assert "This page has iframes" in driver.page_source
#quit the driver
driver.quit()
#demo code for conference
//switch To IFrame using Web Element
IWebElement iframe = driver.FindElement(By.Id("iframe1"));
//Switch to the frame
driver.SwitchTo().Frame(iframe);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//Now we can type text into email field
IWebElement emailE = driver.FindElement(By.Id("email"));
emailE.SendKeys("admin@selenium.dev");
emailE.Clear();/examples/dotnet/SeleniumDocs/Interactions/FramesTest.cs
// 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.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;
namespace SeleniumDocs.Interactions
{
[TestClass]
public class FramesTest
{
[TestMethod]
public void TestFrames()
{
WebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
// Navigate to Url
driver.Url= "https://www.selenium.dev/selenium/web/iframes.html";
//switch To IFrame using Web Element
IWebElement iframe = driver.FindElement(By.Id("iframe1"));
//Switch to the frame
driver.SwitchTo().Frame(iframe);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//Now we can type text into email field
IWebElement emailE = driver.FindElement(By.Id("email"));
emailE.SendKeys("admin@selenium.dev");
emailE.Clear();
driver.SwitchTo().DefaultContent();
//switch To IFrame using name or id
IWebElement iframe1=driver.FindElement(By.Name("iframe1-name"));
//Switch to the frame
driver.SwitchTo().Frame(iframe1);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
IWebElement 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);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//leave frame
driver.SwitchTo().DefaultContent();
Assert.AreEqual(true, driver.PageSource.Contains("This page has iframes"));
//quit the browser
driver.Quit();
}
}
} iframe = driver.find_element(:id, 'iframe1')
driver.switch_to.frame(iframe)
expect(driver.page_source).to include('We Leave From Here')
email_element = driver.find_element(:id, 'email')
email_element.send_keys('admin@selenium.dev')
email_element.clear
driver.switch_to.default_content/examples/ruby/spec/interactions/frames_spec.rb
# 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.
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Frames' do
let(:driver) { start_session }
it 'performs iframe switching operations' do
driver.navigate.to 'https://www.selenium.dev/selenium/web/iframes.html'
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(:id, 'iframe1')
driver.switch_to.frame(iframe)
expect(driver.page_source).to include('We Leave From Here')
email_element = driver.find_element(:id, 'email')
email_element.send_keys('admin@selenium.dev')
email_element.clear
driver.switch_to.default_content
# --- Switch to iframe using name or ID ---
iframe1 = driver.find_element(:name, 'iframe1-name')
driver.switch_to.frame(iframe1)
expect(driver.page_source).to include('We Leave From Here')
email = driver.find_element(:id, 'email')
email.send_keys('admin@selenium.dev')
email.clear
driver.switch_to.default_content
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
expect(driver.page_source).to include('We Leave From Here')
# --- Final page content check ---
driver.switch_to.default_content
expect(driver.page_source).to include('This page has iframes')
end
end
// Store the web element
const iframe = driver.findElement(By.css('#modal > iframe'));
// Switch to the frame
await driver.switchTo().frame(iframe);
// Now we can click the button
await driver.findElement(By.css('button')).click();
// Store the web element
val iframe = driver.findElement(By.id("iframe1"))
// Switch to the frame
driver.switchTo().frame(iframe)
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")/examples/kotlin/src/test/kotlin/dev/selenium/interactions/FramesTest.kt
package dev.selenium.interactions
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
class FramesTest : BaseTest() {
private val framesPage = "https://www.selenium.dev/selenium/web/iframes.html"
@Test
fun switchToFrameByElement() {
driver.get(framesPage)
// Store the web element
val iframe = driver.findElement(By.id("iframe1"))
// Switch to the frame
driver.switchTo().frame(iframe)
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByNameOrId() {
driver.get(framesPage)
// Using the ID
driver.switchTo().frame("iframe1")
driver.switchTo().defaultContent()
// Or using the name instead
driver.switchTo().frame("iframe1-name")
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByIndex() {
driver.get(framesPage)
// Switches to the first frame
driver.switchTo().frame(0)
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun leaveFrame() {
driver.get(framesPage)
driver.switchTo().frame(0)
// Return to the top level
driver.switchTo().defaultContent()
Assertions.assertTrue(driver.pageSource.orEmpty().contains("This page has iframes"))
}
}
Usando um name ou ID
Se o seu frame ou iframe tiver um atributo id ou name, ele pode ser usado alternativamente. Se o name ou ID não for exclusivo na página, o primeiro encontrado será utilizado.
//switch To IFrame using name or id
WebElement iframe1=driver.findElement(By.name("iframe1-name"));
//Switch to the frame
driver.switchTo().frame(iframe1);
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();/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java
// 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 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{
@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
WebElement iframe1=driver.findElement(By.name("iframe1-name"));
//Switch to the frame
driver.switchTo().frame(iframe1);
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();
}
}
# --- Switch to iframe using name or ID ---
iframe1=driver.find_element(By.NAME, "iframe1-name") # (This line doesn't switch, just locates)
driver.switch_to.frame(iframe1)
assert "We Leave From Here" in driver.page_source
email = driver.find_element(By.ID, "email")
email.send_keys("admin@selenium.dev")
email.clear()
driver.switch_to.default_content()/examples/python/tests/interactions/test_frames.py
# 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.
from selenium import webdriver
from selenium.webdriver.common.by import By
#set chrome and launch web page
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/iframes.html")
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(By.ID, "iframe1")
driver.switch_to.frame(iframe)
assert "We Leave From Here" in driver.page_source
email_element = driver.find_element(By.ID, "email")
email_element.send_keys("admin@selenium.dev")
email_element.clear()
driver.switch_to.default_content()
# --- Switch to iframe using name or ID ---
iframe1=driver.find_element(By.NAME, "iframe1-name") # (This line doesn't switch, just locates)
driver.switch_to.frame(iframe1)
assert "We Leave From Here" in driver.page_source
email = driver.find_element(By.ID, "email")
email.send_keys("admin@selenium.dev")
email.clear()
driver.switch_to.default_content()
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
assert "We Leave From Here" in driver.page_source
# --- Final page content check ---
driver.switch_to.default_content()
assert "This page has iframes" in driver.page_source
#quit the driver
driver.quit()
#demo code for conference
//switch To IFrame using name or id
IWebElement iframe1=driver.FindElement(By.Name("iframe1-name"));
//Switch to the frame
driver.SwitchTo().Frame(iframe1);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
IWebElement email = driver.FindElement(By.Id("email"));
//Now we can type text into email field
email.SendKeys("admin@selenium.dev");
email.Clear();/examples/dotnet/SeleniumDocs/Interactions/FramesTest.cs
// 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.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;
namespace SeleniumDocs.Interactions
{
[TestClass]
public class FramesTest
{
[TestMethod]
public void TestFrames()
{
WebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
// Navigate to Url
driver.Url= "https://www.selenium.dev/selenium/web/iframes.html";
//switch To IFrame using Web Element
IWebElement iframe = driver.FindElement(By.Id("iframe1"));
//Switch to the frame
driver.SwitchTo().Frame(iframe);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//Now we can type text into email field
IWebElement emailE = driver.FindElement(By.Id("email"));
emailE.SendKeys("admin@selenium.dev");
emailE.Clear();
driver.SwitchTo().DefaultContent();
//switch To IFrame using name or id
IWebElement iframe1=driver.FindElement(By.Name("iframe1-name"));
//Switch to the frame
driver.SwitchTo().Frame(iframe1);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
IWebElement 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);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//leave frame
driver.SwitchTo().DefaultContent();
Assert.AreEqual(true, driver.PageSource.Contains("This page has iframes"));
//quit the browser
driver.Quit();
}
}
} iframe1 = driver.find_element(:name, 'iframe1-name')
driver.switch_to.frame(iframe1)
expect(driver.page_source).to include('We Leave From Here')
email = driver.find_element(:id, 'email')
email.send_keys('admin@selenium.dev')
email.clear
driver.switch_to.default_content/examples/ruby/spec/interactions/frames_spec.rb
# 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.
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Frames' do
let(:driver) { start_session }
it 'performs iframe switching operations' do
driver.navigate.to 'https://www.selenium.dev/selenium/web/iframes.html'
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(:id, 'iframe1')
driver.switch_to.frame(iframe)
expect(driver.page_source).to include('We Leave From Here')
email_element = driver.find_element(:id, 'email')
email_element.send_keys('admin@selenium.dev')
email_element.clear
driver.switch_to.default_content
# --- Switch to iframe using name or ID ---
iframe1 = driver.find_element(:name, 'iframe1-name')
driver.switch_to.frame(iframe1)
expect(driver.page_source).to include('We Leave From Here')
email = driver.find_element(:id, 'email')
email.send_keys('admin@selenium.dev')
email.clear
driver.switch_to.default_content
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
expect(driver.page_source).to include('We Leave From Here')
# --- Final page content check ---
driver.switch_to.default_content
expect(driver.page_source).to include('This page has iframes')
end
end
// Using the ID
await driver.switchTo().frame('buttonframe');
// Or using the name instead
await driver.switchTo().frame('myframe');
// Now we can click the button
await driver.findElement(By.css('button')).click();
// Using the ID
driver.switchTo().frame("iframe1")
driver.switchTo().defaultContent()
// Or using the name instead
driver.switchTo().frame("iframe1-name")
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")/examples/kotlin/src/test/kotlin/dev/selenium/interactions/FramesTest.kt
package dev.selenium.interactions
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
class FramesTest : BaseTest() {
private val framesPage = "https://www.selenium.dev/selenium/web/iframes.html"
@Test
fun switchToFrameByElement() {
driver.get(framesPage)
// Store the web element
val iframe = driver.findElement(By.id("iframe1"))
// Switch to the frame
driver.switchTo().frame(iframe)
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByNameOrId() {
driver.get(framesPage)
// Using the ID
driver.switchTo().frame("iframe1")
driver.switchTo().defaultContent()
// Or using the name instead
driver.switchTo().frame("iframe1-name")
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByIndex() {
driver.get(framesPage)
// Switches to the first frame
driver.switchTo().frame(0)
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun leaveFrame() {
driver.get(framesPage)
driver.switchTo().frame(0)
// Return to the top level
driver.switchTo().defaultContent()
Assertions.assertTrue(driver.pageSource.orEmpty().contains("This page has iframes"))
}
}
Usando um índice
Também é possível usar o índice do frame, podendo ser consultado usando window.frames em JavaScript.
//switch To IFrame using index
driver.switchTo().frame(0);/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java
// 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 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{
@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
WebElement iframe1=driver.findElement(By.name("iframe1-name"));
//Switch to the frame
driver.switchTo().frame(iframe1);
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();
}
}
driver.switch_to.frame(0)
assert "We Leave From Here" in driver.page_source/examples/python/tests/interactions/test_frames.py
# 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.
from selenium import webdriver
from selenium.webdriver.common.by import By
#set chrome and launch web page
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/iframes.html")
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(By.ID, "iframe1")
driver.switch_to.frame(iframe)
assert "We Leave From Here" in driver.page_source
email_element = driver.find_element(By.ID, "email")
email_element.send_keys("admin@selenium.dev")
email_element.clear()
driver.switch_to.default_content()
# --- Switch to iframe using name or ID ---
iframe1=driver.find_element(By.NAME, "iframe1-name") # (This line doesn't switch, just locates)
driver.switch_to.frame(iframe1)
assert "We Leave From Here" in driver.page_source
email = driver.find_element(By.ID, "email")
email.send_keys("admin@selenium.dev")
email.clear()
driver.switch_to.default_content()
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
assert "We Leave From Here" in driver.page_source
# --- Final page content check ---
driver.switch_to.default_content()
assert "This page has iframes" in driver.page_source
#quit the driver
driver.quit()
#demo code for conference
//switch To IFrame using index
driver.SwitchTo().Frame(0);/examples/dotnet/SeleniumDocs/Interactions/FramesTest.cs
// 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.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;
namespace SeleniumDocs.Interactions
{
[TestClass]
public class FramesTest
{
[TestMethod]
public void TestFrames()
{
WebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
// Navigate to Url
driver.Url= "https://www.selenium.dev/selenium/web/iframes.html";
//switch To IFrame using Web Element
IWebElement iframe = driver.FindElement(By.Id("iframe1"));
//Switch to the frame
driver.SwitchTo().Frame(iframe);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//Now we can type text into email field
IWebElement emailE = driver.FindElement(By.Id("email"));
emailE.SendKeys("admin@selenium.dev");
emailE.Clear();
driver.SwitchTo().DefaultContent();
//switch To IFrame using name or id
IWebElement iframe1=driver.FindElement(By.Name("iframe1-name"));
//Switch to the frame
driver.SwitchTo().Frame(iframe1);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
IWebElement 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);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//leave frame
driver.SwitchTo().DefaultContent();
Assert.AreEqual(true, driver.PageSource.Contains("This page has iframes"));
//quit the browser
driver.Quit();
}
}
} driver.switch_to.frame(0)
expect(driver.page_source).to include('We Leave From Here')/examples/ruby/spec/interactions/frames_spec.rb
# 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.
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Frames' do
let(:driver) { start_session }
it 'performs iframe switching operations' do
driver.navigate.to 'https://www.selenium.dev/selenium/web/iframes.html'
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(:id, 'iframe1')
driver.switch_to.frame(iframe)
expect(driver.page_source).to include('We Leave From Here')
email_element = driver.find_element(:id, 'email')
email_element.send_keys('admin@selenium.dev')
email_element.clear
driver.switch_to.default_content
# --- Switch to iframe using name or ID ---
iframe1 = driver.find_element(:name, 'iframe1-name')
driver.switch_to.frame(iframe1)
expect(driver.page_source).to include('We Leave From Here')
email = driver.find_element(:id, 'email')
email.send_keys('admin@selenium.dev')
email.clear
driver.switch_to.default_content
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
expect(driver.page_source).to include('We Leave From Here')
# --- Final page content check ---
driver.switch_to.default_content
expect(driver.page_source).to include('This page has iframes')
end
end
// Switches to the second frame
await driver.switchTo().frame(1); // Switches to the first frame
driver.switchTo().frame(0)/examples/kotlin/src/test/kotlin/dev/selenium/interactions/FramesTest.kt
package dev.selenium.interactions
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
class FramesTest : BaseTest() {
private val framesPage = "https://www.selenium.dev/selenium/web/iframes.html"
@Test
fun switchToFrameByElement() {
driver.get(framesPage)
// Store the web element
val iframe = driver.findElement(By.id("iframe1"))
// Switch to the frame
driver.switchTo().frame(iframe)
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByNameOrId() {
driver.get(framesPage)
// Using the ID
driver.switchTo().frame("iframe1")
driver.switchTo().defaultContent()
// Or using the name instead
driver.switchTo().frame("iframe1-name")
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByIndex() {
driver.get(framesPage)
// Switches to the first frame
driver.switchTo().frame(0)
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun leaveFrame() {
driver.get(framesPage)
driver.switchTo().frame(0)
// Return to the top level
driver.switchTo().defaultContent()
Assertions.assertTrue(driver.pageSource.orEmpty().contains("This page has iframes"))
}
}
Deixando um frame
Para deixar um iframe ou frameset, volte para o conteúdo padrão como a seguir:
//leave frame
driver.switchTo().defaultContent();/examples/java/src/test/java/dev/selenium/interactions/FramesTest.java
// 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 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{
@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
WebElement iframe1=driver.findElement(By.name("iframe1-name"));
//Switch to the frame
driver.switchTo().frame(iframe1);
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();
}
}
driver.switch_to.default_content()
assert "This page has iframes" in driver.page_source/examples/python/tests/interactions/test_frames.py
# 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.
from selenium import webdriver
from selenium.webdriver.common.by import By
#set chrome and launch web page
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/iframes.html")
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(By.ID, "iframe1")
driver.switch_to.frame(iframe)
assert "We Leave From Here" in driver.page_source
email_element = driver.find_element(By.ID, "email")
email_element.send_keys("admin@selenium.dev")
email_element.clear()
driver.switch_to.default_content()
# --- Switch to iframe using name or ID ---
iframe1=driver.find_element(By.NAME, "iframe1-name") # (This line doesn't switch, just locates)
driver.switch_to.frame(iframe1)
assert "We Leave From Here" in driver.page_source
email = driver.find_element(By.ID, "email")
email.send_keys("admin@selenium.dev")
email.clear()
driver.switch_to.default_content()
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
assert "We Leave From Here" in driver.page_source
# --- Final page content check ---
driver.switch_to.default_content()
assert "This page has iframes" in driver.page_source
#quit the driver
driver.quit()
#demo code for conference
//leave frame
driver.SwitchTo().DefaultContent();/examples/dotnet/SeleniumDocs/Interactions/FramesTest.cs
// 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.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;
namespace SeleniumDocs.Interactions
{
[TestClass]
public class FramesTest
{
[TestMethod]
public void TestFrames()
{
WebDriver driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
// Navigate to Url
driver.Url= "https://www.selenium.dev/selenium/web/iframes.html";
//switch To IFrame using Web Element
IWebElement iframe = driver.FindElement(By.Id("iframe1"));
//Switch to the frame
driver.SwitchTo().Frame(iframe);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//Now we can type text into email field
IWebElement emailE = driver.FindElement(By.Id("email"));
emailE.SendKeys("admin@selenium.dev");
emailE.Clear();
driver.SwitchTo().DefaultContent();
//switch To IFrame using name or id
IWebElement iframe1=driver.FindElement(By.Name("iframe1-name"));
//Switch to the frame
driver.SwitchTo().Frame(iframe1);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
IWebElement 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);
Assert.AreEqual(true, driver.PageSource.Contains("We Leave From Here"));
//leave frame
driver.SwitchTo().DefaultContent();
Assert.AreEqual(true, driver.PageSource.Contains("This page has iframes"));
//quit the browser
driver.Quit();
}
}
} driver.switch_to.default_content
expect(driver.page_source).to include('This page has iframes')/examples/ruby/spec/interactions/frames_spec.rb
# 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.
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Frames' do
let(:driver) { start_session }
it 'performs iframe switching operations' do
driver.navigate.to 'https://www.selenium.dev/selenium/web/iframes.html'
# --- Switch to iframe using WebElement ---
iframe = driver.find_element(:id, 'iframe1')
driver.switch_to.frame(iframe)
expect(driver.page_source).to include('We Leave From Here')
email_element = driver.find_element(:id, 'email')
email_element.send_keys('admin@selenium.dev')
email_element.clear
driver.switch_to.default_content
# --- Switch to iframe using name or ID ---
iframe1 = driver.find_element(:name, 'iframe1-name')
driver.switch_to.frame(iframe1)
expect(driver.page_source).to include('We Leave From Here')
email = driver.find_element(:id, 'email')
email.send_keys('admin@selenium.dev')
email.clear
driver.switch_to.default_content
# --- Switch to iframe using index ---
driver.switch_to.frame(0)
expect(driver.page_source).to include('We Leave From Here')
# --- Final page content check ---
driver.switch_to.default_content
expect(driver.page_source).to include('This page has iframes')
end
end
// Return to the top level
await driver.switchTo().defaultContent(); // Return to the top level
driver.switchTo().defaultContent()/examples/kotlin/src/test/kotlin/dev/selenium/interactions/FramesTest.kt
package dev.selenium.interactions
import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.openqa.selenium.By
class FramesTest : BaseTest() {
private val framesPage = "https://www.selenium.dev/selenium/web/iframes.html"
@Test
fun switchToFrameByElement() {
driver.get(framesPage)
// Store the web element
val iframe = driver.findElement(By.id("iframe1"))
// Switch to the frame
driver.switchTo().frame(iframe)
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByNameOrId() {
driver.get(framesPage)
// Using the ID
driver.switchTo().frame("iframe1")
driver.switchTo().defaultContent()
// Or using the name instead
driver.switchTo().frame("iframe1-name")
// Now we can type text into the email field
val email = driver.findElement(By.id("email"))
email.sendKeys("admin@selenium.dev")
Assertions.assertEquals("admin@selenium.dev", email.getAttribute("value"))
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun switchToFrameByIndex() {
driver.get(framesPage)
// Switches to the first frame
driver.switchTo().frame(0)
Assertions.assertTrue(driver.pageSource.orEmpty().contains("We Leave From Here"))
}
@Test
fun leaveFrame() {
driver.get(framesPage)
driver.switchTo().frame(0)
// Return to the top level
driver.switchTo().defaultContent()
Assertions.assertTrue(driver.pageSource.orEmpty().contains("This page has iframes"))
}
}



