Thursday, 2 May 2013

Handle iFrames | Selenium

It's easy to work with iFrames.  First, try to find all the iFrames available in web page;  Next, switch into the iframe and then come out of the iframe.


Snippet

try{
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id='Value']")));
      new WebDriverWait(driver, 5)
      .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("Value")));
System.out.println("Element found");
}catch (TimeoutException e) {
      System.out.println("Element not Found");     
  }
}

How to Terminate iFrame?
To return back from the current iframe to main content, make use of the following script,

driver.switchTo().defaultContent(); 

Handle iFrames without id | name
 
driver.switchTo().frame(0);




Check iFrames availability

Google Chrome
Use Chrome Developer debugging Tool to find all the available iFrames in the web page.




1| Open chrome web Browser
2| Press F12 key
3| Press Esc key
4| In console, you will see a filter icon followed by the dropdown <top frame>
5| Click on the dropdown to see the iFrames availability.

mozilla Firefox
Use the Firefox addon, web-developer to find all the available iframes in a web page. Selecting 'Outline Frames' lets you highlight the web page UI with iframes.



2 comments:

  1. hi, without using Thread.sleep(waittime) how to write the code ? pls sahre ur ideas.

    ReplyDelete