- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵單擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理IFrames
- Selenium - 處理Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態Web表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援功能
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 雜項
- Selenium - 處理Ajax呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV資料檔案
- Selenium - Excel資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 雜項概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 處理IFrames
Selenium Webdriver 可用於處理網頁上的 iframe。iframe(稱為內聯框架)基本上是 HTML 5 中包含的 html 標籤。一個iframe 標籤用於在另一個HTML文件中包含 HTML 文件。
HTML 中的 frame 和 iframe 標籤之間存在細微差別。frame 標籤可以將網頁垂直和水平方向分割,而 iframe 標籤用於在另一個 HTML 文件中包含 HTML 文件。但是,從 HTML 5 版本開始,不再使用 frame 概念。
網頁上 iframe 的識別
開啟 Chrome 瀏覽器,右鍵單擊網頁,然後單擊“檢查”按鈕。然後,整個頁面的完整 HTML 程式碼現在就可以訪問了。要調查頁面上的 iframe,請單擊左側向上的箭頭,該箭頭位於可見 HTML 的頂部,如下所示。
一旦我們單擊並將箭頭指向Selenium - Automation Practice Form(在Iframe 1下方),其 HTML 程式碼就可見了,顯示了文字Selenium- Automation Practice Form位於 iframe 標籤名內的事實。
並且文字Selenium- Automation Practice Form出現在標題標籤內(稱為“h1”並用<>括起來)。
<h1>Selenium - Automation Practice Form</h1>
在上面的頁面中,我們將獲取文字Selenium - Automation Practice Form,該文字位於 iframe 內。我們觀察到該文字出現在頁面上的第一個 iframe 內,因此其索引將為 0。
為了訪問 html 中 iframe 標籤內的 web 元素,webdriver 應該能夠首先找到頁面內所有 iframe,然後找到它們內部的專案。為了實現上述目的,我們必須將驅動程式上下文從主瀏覽器切換到內部的 iframe。
在 Selenium 中處理 iframe 的基本方法
下面列出了處理 iframe 的基本過載方法:
switchTo.frame(args)
iframe 索引號作為引數傳遞給方法。iframe 的起始索引為 0。驅動程式切換到傳遞給方法的 iframe 編號。
語法
driver.switchTo.frame(0), switching to the first iframe.
switchTo.frame(args)
iframe id 或名稱作為引數傳遞給方法。驅動程式切換到傳遞給方法的 iframe id 或名稱。
語法
driver.switchTo.frame(“id”), switching to the iframe having id or name value as id.
switchTo.frame("args")
iframe webelement 作為引數傳遞給方法。驅動程式切換到傳遞給方法的 iframe webelement。
語法
driver.switchTo.frame("id"), switching to the
iframe having webelement value as id.
driver.switchTo.defaultContent()
將驅動程式上下文從 iframe 切換到主網頁。
示例 1
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class Iframe {
public static void main(String[] args) throws InterruptedException {
//Initiate the Webdriver
WebDriver driver = new ChromeDriver();
//adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Opening the webpage where we will access iframes
driver.get("https://tutorialspoint.tw/selenium/practice/frames.php");
//switch to an iframe with first iframe index
driver.switchTo().frame(0) ;
// identify the text inside the iframe and retrieve with getText()
String text = driver.findElement(By.tagName("h1")).getText() ;
System.out.println(" Text is: " + text);
//switch back the driver out of the iframe to the main page
driver.switchTo().defaultContent();
//quitting the browser
driver.quit();
}
}
輸出
Text is: Selenium - Automation Practice Form Process finished with exit code 0
在上面的示例中,我們已經識別了網頁上可用的 iframe 標籤,並將驅動程式上下文從主頁面切換到該 iframe。一旦實現這一點,我們已經訪問了該 iframe 內可用的文字,控制檯中的訊息為 - 文字為:Selenium - Automation Practice Form。
最後,收到訊息Process finished with exit code 0,表示程式碼已成功執行。
因此,我們能夠在網頁中識別 iframe。除此之外,我們還實現瞭如何使用 Selenium webdriver 中可用的 switchTo 方法將驅動程式的上下文從主頁面切換到 iframe。
示例 2
讓我們以以下頁面為例,其中有兩個 iframe。我們可以透過識別標籤名為 iframe 的元素並將其與 findElements(By.tagname()) 方法一起使用,來計算網頁上 iframe 的總數。這將返回頁面上 iframe 的列表。最後,列表的大小將幫助我們計算 iframe 的總數。
程式碼實現
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class CountIframe {
public static void main(String[] args) throws InterruptedException {
//Initiate the Webdriver
WebDriver driver = new ChromeDriver();
//adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Opening the webpage where we will access iframes
driver.get("https://tutorialspoint.tw/selenium/practice/frames.php");
// count total iframes
List<WebElement> f = driver.findElements(By.tagName("iframe"));
int total = f.size();
System.out.println("Total iframes: " + total);
//quitting the browser
driver.quit();
}
}
輸出
Total iframes: 2
在上面的示例中,我們已經計算了頁面上 iframe 的總數,控制檯中的訊息為 - iframe 總數:2。
結論
這總結了我們關於 Selenium Webdriver 處理 iframe 的教程的全面概述。我們從描述如何在網頁上識別 iframe、Selenium 中處理 iframe 的基本方法開始,並逐步介紹瞭如何使用 Selenium Webdriver 處理 iframe 的示例。這使您能夠深入瞭解 Selenium Webdriver 處理 iframe。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。
