How to deal with #document tag under iframe

I want to operate on an element on a website, which is inside an iFrame and further inside a document tag. How to do that?

2 Likes

Are you referring to a structure like this?

<iframe id="FRAMENAME">
#document
    <html>
        CODE....
    </html>
1 Like

Yes. How to work with these?

Have you tried using XPath?

1 Like

Yes. I did. I tried to copy the XPath from the webpage and use it, but due to this tag, it is not pointing on the correct element

1 Like

Ok got it. You need to switch to particular iFrame before accessing its elements. Simply add a switchTo like this:

driver.switchTo().frame("FRAMENAME");
1 Like

Thanks @rishabhps

will try it out

1 Like

Sure Tim. Please let me know if it works for you

1 Like

@rishabhps It didnt work

I am getting a NoSuchElementException

You might want to insert a wait for your IFrame and element to load.

You can also use

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("FRAMENAME")));

It will wait until the IFrame becomes available

1 Like

Great. It worked.

Thanks a lot @rishabhps

1 Like

Is their any other way to do this? Just curious

No Tim.

At the moment, Selenium does not have any other ways to work with iframe wrappers.

Hence you need to switch to specific one, to work with them.

You can also refer Selenium Focus Element Issues And How To Solve Them? | LambdaTest to learn more

1 Like

ok. thanks for the help

1 Like