如何在 Selenium 中統計頁面中的幀數?
我們可以透過以下方法統計 Selenium 中的幀數
使用標記名稱 frame/iframe 的 List<WebElement> 的幫助。
藉助 Javascript 執行器。
示例
使用標記名。
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class FrameCount{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "http://the-internet.herokuapp.com/nested_frames"; driver.get(url); driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); //By finding list of the web elements using frame or iframe tag List<WebElement> f = driver.findElements(By.tagName("frame")); System.out.println("Total number " + f.size()); driver.quit(); } }
示例
使用 Javascript 執行器。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.JavascriptExecutor; import java.util.concurrent.TimeUnit; public class FrameCountJS{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "http://the-internet.herokuapp.com/nested_frames"; driver.get(url); driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); //Javascript executor script to get the window length JavascriptExecutor exe = (JavascriptExecutor) driver; int f = Integer.parseInt(exe.executeScript("return window.length").toString()); System.out.println("No. of iframes on the page are " + f); driver.quit(); } }
廣告