使用 IFrames 和框架
框架是一种现在已弃用的方法,用于从同一域上的多个文档构建网站布局。除非您正在使用 HTML5 之前的 Web 应用程序,否则您不太可能使用它们。 IFrames 允许插入来自完全不同域的文档,并且仍然很常用。
如果您需要使用框架或 iframe,WebDriver 允许您以相同的方式使用它们。考虑一个 iframe 中的按钮。如果我们使用浏览器开发工具检查该元素,我们可能会看到以下内容
<div id="modal">
<iframe id="buttonframe" name="myframe" src="https://seleniumhq.github.io">
<button>Click here</button>
</iframe>
</div>
如果不是 iframe,我们期望使用如下代码点击按钮
//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()
但是,如果 iframe 之外没有按钮,您可能会收到“找不到元素”错误。 发生这种情况是因为 Selenium 只知道顶级文档中的元素。 要与按钮交互,我们需要先切换到框架,类似于我们切换窗口的方式。 WebDriver 提供了三种切换到框架的方法。 以下示例代码演示了我们如何使用实时 Web 示例来完成此操作。
使用 WebElement
使用 WebElement 切换是最灵活的选择。 您可以使用首选的选择器找到框架并切换到它。
//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();
# Store iframe web element
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
# switch to selected iframe
driver.switch_to.frame(iframe)
# Now click on button
driver.find_element(By.TAG_NAME, 'button').click()
//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();
# Store iframe web element
iframe = driver.find_element(:css,'#modal > iframe')
# Switch to the frame
driver.switch_to.frame iframe
# Now, Click on the button
driver.find_element(:tag_name,'button').click
// 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.cssSelector("#modal>iframe"))
//Switch to the frame
driver.switchTo().frame(iframe)
//Now we can click the button
driver.findElement(By.tagName("button")).click()
使用名称或 ID
如果您的框架或 iframe 具有 id 或 name 属性,则可以使用此属性代替。 如果页面上的名称或 ID 不是唯一的,则将切换到找到的第一个。
//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();
# Switch frame by id
driver.switch_to.frame('buttonframe')
# Now, Click on the button
driver.find_element(By.TAG_NAME, 'button').click()
//switch To IFrame using name or id
driver.FindElement(By.Name("iframe1-name"));
//Switch to the frame
driver.SwitchTo().Frame(iframe);
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();
# Switch by ID
driver.switch_to.frame 'buttonframe'
# Now, Click on the button
driver.find_element(:tag_name,'button').click
// 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("buttonframe")
//Or using the name instead
driver.switchTo().frame("myframe")
//Now we can click the button
driver.findElement(By.tagName("button")).click()
使用索引
也可以使用框架的索引,例如可以使用 JavaScript 中的 window.frames 查询。
//switch To IFrame using index
driver.switchTo().frame(0);
# Switch to the second frame
driver.switch_to.frame(1)
//switch To IFrame using index
driver.SwitchTo().Frame(0);
// Switches to the second frame
await driver.switchTo().frame(1);
// Switches to the second frame
driver.switchTo().frame(1)
离开框架
要离开 iframe 或框架集,请切换回默认内容,如下所示
//leave frame
driver.switchTo().defaultContent();
# switch back to default content
driver.switch_to.default_content()
//leave frame
driver.SwitchTo().DefaultContent();
# Return to the top level
driver.switch_to.default_content
// Return to the top level
await driver.switchTo().defaultContent();
// Return to the top level
driver.switchTo().defaultContent()