Search for an iframe on a web Page

How to find an iframe is present on the web page or not?

The Html tag is given below

<div class="bluebox">
   <div class="title"><span>Preview</span></div>
   <div class="content">
      <iframe frameborder="0" src="about:blank" style="width:100%;height:488px;border:0 none;" id="previewFrame"></iframe>
   </div>
   <div class="box_shadow">
      <div class="left"></div>
      <div class="right"></div>
   </div>
</div>

In my code I have written like :

driver.switchTo().frame(driver.findElement(By.id("previewFrame")));
System.out.println("Successfully switched to frame.");

boolean checkStatus = driver.findElement(By.id("previewFrame")).isDisplayed();

Error: Not able to find the iframe

Upvotes: 0

Views: 98

Answers (3)

StrikerVillain
StrikerVillain

Reputation: 3776

try switching to the default page and then performing switching to the required frame.

driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.id("previewFrame")));

Let me know if this helps you. Thanks.

Upvotes: 0

Ajinkya
Ajinkya

Reputation: 22710

You can do something like

boolean isExists = true;
try {
       driver.findElement(By.id("previewFrame");
       // No exception means frame is present
    } catch (NoSuchElementException e) {
       // Exception, no such frame is present
       isExists = false;
    }

Upvotes: 1

Richard
Richard

Reputation: 9019

Once you switch to the iframe, Selenium's point of reference is everything contained in the iframe.

To check if Selenium has the correct reference, test for an element inside of the iframe.

As a side note, you can simplify your switchTo:

driver.switchTo().frame("previewFrame");

Upvotes: 0

Related Questions