How to switch to frame and switch back to default content?
Like our facebook page www.facebook.com/ankprotraining
What is a frame?
Frame is a web page which is embedded in another web page or an HTML document embedded inside another HTML document.
The Frame is often used to insert content from another source, such as an advertisement, into a Web page.
The iframe tag specifies an inline frame.
How to switch to frame in selenium webdriver?
We can identify the iframes using methods given below:
Right click on the element, if you find the option like ‘View frame source' then it is an iframe.
Right click on the page and click 'View Page Source' and Search with the 'iframe', if you can find any tag name with the 'iframe' then it is meaning to say the page consisting an iframe.
Usage: driver.switchTo().frame("iframe_a");
How To Switch Back To Main Window using switch to default content?
To get back to the main you can use
driver.switchTo().defaultContent();
Selects either the first frame on the page or the main document when a page contains iFrames.
Possible Interview Questions on switch to frame in selenium webdriver :
How to handle frames in selenium webdriver?
How to navigate back to main Window after switching into Frames?
Code :
@Test
public void SwitchToFrame() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("[ Ссылка ]");
driver.switchTo().frame("iframe_a");
driver.findElement(By.id("name")).sendKeys("Ankpro");
Thread.sleep(2000);
driver.switchTo().defaultContent();
driver.findElement(By.linkText("uitestpractice.com")).click();
Thread.sleep(3000);
driver.quit();
}
Ещё видео!