enter image description here

I want to get an element's id attribute that is inside of an iframe through Java. How do I access an element inside of an iframe in Java?

  • I am little worried to see what you are automating. – Tarun Dec 8 '13 at 16:41

In my experience the simplest way to manipulate elements inside of an iframe with web automation is to treat the iframe src URL as a separate page and call the driver to visit that URL instead of the parent page. From the example in the screenshot, I would have an automated step that would visit the URL in the src attribute of iframe#ComposeRteEditor_surface... (the actual src attribute wasn't visible in the screenshot), then I would call the appropriate step or findElement() function as normal.

  • Thanks for ur answer, I tried in the following way but i did not get the expected value. driver.switchTo().frame(driver.findElement(By.id("ComposeRteEditor_surface"))).findElement(By.tagName("img")).toString(); – user6258 Nov 8 '13 at 7:48
  • Instead of driver.switchTo() try using driver.get() with the src attribute as the URL parameter. Then you can use findElementById() as normal. – Francis Nov 8 '13 at 15:47
  • user6258, when you say you don't get the expected value, what do you get? Is there an exception? Is it having trouble finding the iframe? Is it finding the iframe but having trouble finding the element within it? Something else? – Sam Woods Nov 8 '13 at 17:58

You should try out the TestProject test recorder which can locate elements within iFrames, so you’ll be able to inspect elements and their attributes easily.

  • If someone is already using Selenium, is using TestProject as simple as just adding another library? Or is there a migration path? Also, generally when suggesting a new library you should provide a code example. – corsiKa Sep 16 '20 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.