I am writing a Selenium testcase for a webpage which has multiple frames and WebElements. My goal is to get a specific WebElement by xpath without knowing anything about the iframes before starting the testcase.

What would be the best way to identify the iframe in which the WebElement I am searching for is located?

I created a pseudocode function which might work, but I am not sure if it's the best way:

  1. Create a list with all iFrames found via the html-frame tag
  2. Iterate through every list
  3. set the driver focus to the list element
  4. search for the WebElement I want to have
  5. If WebElement is not null return it

Is there any better way to solve this problem?

  • 1
    If there is no unique identifier , thats a good approach you have there. The right thing will be to ask dev team to add unique ID to the frame in which it appears . Else the test would be so brittle
    – PDHide
    May 21, 2020 at 13:25
  • 1
    Each iframe holds a separate dom tree. How would you know which element is the proper one if you would have the element in several frames?
    – Alexey R.
    May 21, 2020 at 14:56
  • I would try to make sure that the xpath of the element I am searching for is unique
    – D. Jung
    May 21, 2020 at 15:13


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.