0
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Iframe {

public static void main(String[] args) throws Exception{
    WebDriver driver=new FirefoxDriver();
    try{
    driver.get("http://www.timesjobs.com/candidate/logout.html");
    driver.manage().window().maximize();
    driver.findElement(By.xpath("//li[1][@class='bdr-left']/a")).click();
            //Once the button is clicked a window is open and when i checked it says that its a iframe
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    
    WebElement wb=driver.findElement(By.xpath("iframe[@id='GB_frame1']"));
    driver.switchTo().frame(wb);
    System.out.println("Frame Name ="+driver.getTitle());
    driver.switchTo().defaultContent();
    System.out.println("Current Page Is "+driver.getTitle());
    }//try
    catch(Exception e){
        e.printStackTrace();
        driver.close();
        driver.quit();
    }//catch
    finally{
        driver.close();
        driver.quit();
    }//finally
 }//main
}//class

Here i am trying to send some value in the login id feild but when i am clicking ti signin button a popup opens. When i right click on the popup then i got a option which says that THIS FRAME then i come to know that its a frame. I tried to switch to it by using driver.switchTo().frame(wb); where wb is having the path of the frame. When i run the code i got NOSuchElement exception for the loginid feils which means the webdriver is not able to pass the control to the frame.

How can i solve this?

Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56

3 Answers3

0

Are you sure you're trying to switch to an iframe instead of switching window handles? Usually when there is a popup you should be switching windows. In any case, you should at least switch to the popup first before finding the iframe.

Caren
  • 1,358
  • 2
  • 13
  • 22
0

Your problem is here:

driver.findElement(By.xpath("iframe[@id='GB_frame1']"));

That XPath is not correct, it should be something like:

driver.findElement(By.xpath("//iframe[@id='GB_frame']"));

or the shortcut:

driver.findElement(By.id("GB_frame"));
SiKing
  • 10,003
  • 10
  • 39
  • 90
  • @Shantanu I think you might be confused about the difference between "inappropriate" and "incorrect". – SiKing Sep 21 '14 at 16:05
0

After doing a lot of research I found that there is a frame inside a frame and I was trying to access the inner frame elements from the outer frame. The code which I added in my existing code is below

 wb=driver.findElement(By.xpath(".//*[@id='GB_window']/div[2]/iframe[@id='GB_frame1']"));
    driver.switchTo().frame(wb);
//This is taking me to outer frame
    wb=driver.findElement(By.xpath("//iframe[@id='GB_frame']"));
    driver.switchTo().frame(wb);
//This is taking me to inner frame which is inside the outer frame
    System.out.println("Frame Name ="+driver.getTitle());
    wb=driver.findElement(By.xpath(".//*[@id='j_username']"));
    wb.click();
    wb.sendKeys("sdfs");

Finally the code is working as per my expectation

Jon Lauridsen
  • 2,521
  • 5
  • 31
  • 38
Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56