0

In my project there is an iframe whose id is changed every time we click on that

I tried numerous ways to work on that

1)         List<WebElement> frameList=driver.findElements(By.tagName("iframe"));
           System.out.println(frameList.size());

          for(int i =0; i<(frameList.size());i++){
       driver.switchTo().frame(i);
       System.out.println(i);
       try{
            driver.findElement(By.id("TextBox_4")).sendKeys("987654321");
            System.out.println("Element found");
        }
       catch (NoSuchElementException e){
       System.out.println("Element not found");
        }

//this will check element in iframe.

2)          WebElement frame =
                   driver.findElement(By.xpath("//div[contains(@id,'title')]"));
            driver.switchTo().frame(frame);

// this search frame using xpath and contains keyword but its showing element not found exception

3)          int frameSize = driver.findElements(By.tagName("iframe")).size();
            System.out.println(frameSize);
            driver.switchTo().frame(2);

//I print the size of frame(i.e. 3) and tried each and every frame value (i.e. 0,1,2)

Please correct wrt above code.

Thanks Diwakar

  • 1
    What are some examples of the IDs of the `IFRAME`, e.g. how does it change? There may be a way to access it anyway. Is the `IFRAME` always in the same place in the HTML? e.g. inside some element that can be identified consistently? – JeffC Oct 10 '16 at 15:31
  • In addition to what JeffC said, please share your webpage with us. Its hard to help without any details. – MasterJoe Oct 10 '16 at 19:29
  • Try using **[frameToBeAvailableAndSwitchToIt](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#frameToBeAvailableAndSwitchToIt-org.openqa.selenium.By-)** method by applying [Explicit wait](http://stackoverflow.com/a/14515517/4193730). It would rather simplify things for you. To make things work first locate the iframe in HTML tab of Firebug, make your xpath for the iFrame and then use the aforementioned method. – Subh Oct 11 '16 at 07:12

1 Answers1

0

If you have fixed number of iframes, you could use that specific index for accessing your specific iframe. Let, you have five iframes and you want to select 3rd iframe then switch to it by using

driver.switchTo().frame(2);

I think, that might solve your problem.

Saifur
  • 16,081
  • 6
  • 49
  • 73
optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37