Selenium - how to get runtime generated iframe id

I'm testing a webpage as a blackbox (hard requirement)

the page has a button, on click it calls a function that generates an iframe with a dynamic id, i.e clicking on the same button will create the same frame content but every time with different

<input type="button" value="some txt" onclick="return displayIframe(if_42222440278);">

this creates an iframe like this

<iframe id="6356469882593" class="someClass" scrolling="no" frameborder="0" src="/Pages/somePage.aspx', '', 'width=740px,height=629&rand=6356469882593" allowtransparency="true">

how can I get the iframe id??

these are my tries so far:

attempt 1: resulted in array of 10 objects all null

List<WebElement> iframes = webDriver.findElements("iframe")); 

attempt 2: resulted in exception that the element doesn't exist

WebElement editable = webDriver.switchTo().activeElement(); 

any other suggestions?


Please try below.

List<WebElement> elements = driver.findElements(By.tagname("iframe"))
//do your stuff


iframeID = driver.findElement(By.cssSelector("iframe[src*='somePage.aspx']")).getAttribute("id");

Try following code:

List<WebElement> we = driver.findElements(By.xpath("//iframe[@class='someClass']"));
driver.switchTo().frame(we.get(0)); //Switch to iframe.
WebElement activeElement = driver.switchTo().activeElement();
/*Do anything here.
driver.switchTo().defaultContent(); //Switch to original frame.

You can identify iframe, switch to it, work in it and switch back to your default content.

Need Your Help

Can't Create Facebook App

facebook-apps facebook-app-requests

First time trying to create a Facebook app. Browsed to and it had a link for to register before creating a link where you just accept the T&amp;C's. Now it s...