InterviewSolution
| 1. |
How to switch from one frame to another frame? |
|
Answer» The frame is a web page embedded in another web page or an HTML document embedded in another HTML document. The IFrame is frequently used to insert content from another source into a Web page. The <iframe> tag specifies an inline frame. The frame inside the frame is called nested frames. To switch from one frame to another first switch to the outer frame by either index or ID of the iframe. Once you switch to the outer frame you can find the total number of iframes inside the outer frame and then switch to the inner frame by any known method. You should FOLLOW the same order as we entered into the frame while exiting that is an exit from the inner frame first and then the outer frame. Syntax to switch frame are: DRIVER.switchTo().frame(0); (switch to outer frame) size = driver.findElements(By.tagName("iframe")).size(); (get iframe count) driver.switchTo().frame(0); (Switching to inner frame) driver.switchTo().parentFrame(); (to move back to parent frame) driver.switchTo().defaultContent(); (to move back to main frame, mostly parent frame)Code to switch to a frame whose ID or web element is not available using index- import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class IndexOfIframe { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("HTTPS://seleniumautomationpractice.blogspot.com/"); driver.manage().window().maximize(); //driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); int size = driver.findElements(By.tagName("iframe")).size(); for(int i=0; i<=size; i++){ driver.switchTo().frame(i); int total=driver.findElements(By.tagName("iframe")).size(); System.out.println(total); driver.switchTo().defaultContent(); } } } |
|