Search code examples
javahtmlseleniumiframewebdriver

How do I access the iframe that does not have an id, name or class


I have the following iframe:


<iframe _ngcontent-oyp-c7="" allow="fullscreen" allowfullscreen="" mozallowfullscreen="" scrolling="yes" webkitallowfullscreen="" src="https://launch.spribegaming.com/games/launch/aviator?user=164291&amp;token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJQdW50ZXJJZCI6IjE2NDI5MSIsIm5iZiI6MTY0MzcyMjU5NCwiZXhwIjoxNjQzNzQ0MTk0LCJpYXQiOjE2NDM3MjI1OTR9.7st61IJ2K4nOHIhezeg9exDUk-mh0t21e73Dq5QQdbY&amp;lang=EN&amp;currency=ZAR&amp;operator=hollywoodbets&amp;return_url=https://new.hollywoodbets.net" frameborder="0"></iframe>

How do I get its element as it doesn't have an Id, name or class. As there another way to access it? It a single iframe, so I cannot call it by index like switchTo.frame(int frame number)


Solution

  • driver.switchTo.frame accepts web-element object argument too.

    frame_element = driver.find_element(...)
    driver.switchTo.frame(frame_element)
    

    For locating the iframe in this case I suggest using XPATH.

    For a single iframe just

    //iframe
    

    For multiple iframes you have to find some unique attributes filter

    //iframe[@_ngcontent-oyp-c7]
    

    or

    //iframe[contains(@src, 'spribegaming.com/games')]
    

    or even combine conditions with or/and xpath operators:

    //iframe[(@scrolling='yes') or (@allow='fullscreen')]