如何在 Selenium 中处理框架?
借助以下方法,我们可以在 Selenium 中处理框架 −
switchTo().frame( frameNumber)
此方法将框架 id 用作参数。框架 id 的索引从 0 开始。如果没有找到框架,将引发 NoSuchFrameException 异常。
switchTo().frame( frameName)
此方法将开发者定义的框架名称用作参数。框架名称被视为字符串,并用引号括起来。如果没有找到框架,将引发 NoSuchFrameException 异常。
switchTo().frame( WebElement)
此方法将 Web 元素用作参数。如果没有找到框架,将引发 NoSuchFrameException 异常。如果框架不再处于活动状态,将引发 StaleElementReferenceException 异常。
示例
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; public class FrameSet { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "url with frames"; driver.get(url); driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); //grabbing the first frame with the help of index driver.switchTo().frame(0); //grabbing the frame with the help of frame name driver.switchTo().frame(“<<name expression>>“); //grabbing the frame with the help of frame webelement WebElement name = driver.findElement(By.name(“frame-right”)); driver.switchTo().frame(driver.findElement(By.name(“<<name expression”))); driver.quit(); } }
广告