我们可以通过下面列出的方法计算硒中的帧数-
借助具有标签名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); //通过使用frame或iframe标签查找网络元素列表 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执行脚本获取窗口长度 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(); } }