iRunner iRunner - 1 month ago 13
Java Question

How to identify the page has iframe in Selenium using Java Code?

I am new to Selenium. The webpages, I have to automate, have iframes for some pages and not for the rest. How to identify whether there is iframe or not so as for that particular page in java only and not from the firebug. If the page has iframe use

webdriver.switchTo().defaultContent();
else check for xpath directly

Answer

Open the page in browser and see HTML code for the page using Firebug, Developer Tools etc. If HTML has tag iframe, that means your page has iframe and hence you'll have to use driver.switchTo.frame(frame); to interact with elements within the frame.

If you want to check with Java code, do following:

driver.getPageSource().contains("iframe"); //this will return true if there is iframe
Comments