[java] Find elements inside forms and iframe using Java and Selenium WebDriver

I'm trying to access elements that are present under <form> <iFrame> <form> elements </form> </iFrame> </form>.

Could you help me on accessing these 'elements', which I'm working with Selenium Webdriver and JAVA?

Issue Encountered: Able to reach the destination page (where the above elements are present), but those elements are not recognized with my code.

Overview of XML structure:

<body>
    <form action="https://abcd/efgh/" name="outerForm" method="post" target="iFrameTitle">
        <iframe width="700" height="600" src="" title="Frame for Java Test" name="iFrameTitle" scrolling="auto" frameborder="0">
            <form id="innerFormID" name="innerForm" action="/xxx/xxxx/yyyy/zzzz/" method="post" autocomplete="off">
                <fieldset id="ncDetailsInner">
                    <div id="element1">
                        <label for="label1">
                        <abbr title="Required field">*</abbr></label>
                        <input name="label2" type="text" maxlength="30" id="cardHolder" value="" >
                    </div>

                    <div id="element2">
                        <label for="label3">Label3 <abbr title="Required field">*</abbr></label>
                        <div id="element3">
                            <label for="label4">Label4<abbr title="Required field">*</abbr></label>
                            <input id="label5" name="labelname5" type="text" maxlength="19" value="">
                        </div>

                        <div id="element4">
                            <label for="label6">Label6</label>
                            <input id="label7" name="labelname7" type="text" size="2" maxlength="2" value="" class="text disabled" disabled="">
                        </div>
                    </div>
                </fieldset>
            </form> 

        </iframe>
    </form>
</body>

Code Extract:

WebDriverWait wait_iframe = new WebDriverWait(driver, 20000);

wait_iframe.until(ExpectedConditions.visibilityOfElementLocated((By.id("element2"))));

calling_function(sh1.getCell(col + 10, row).getContents(), 
                sh1.getCell(col + 11, row).getContents(),
                sh1.getCell(col + 12, row).getContents(), 
                sh1.getCell(col + 14, row).getContents());                      

public static void called_funciton(String string1, String string2,
        String string3, String string4) {
        driver.findElement(By.name("Element1 Name")).sendKeys(string1);
        driver.findElement(By.id("Element2 ID")).sendKeys(string2);
        driver.findElement(By.id("Element3 ID")).sendKeys(string3);
        driver.findElement(By.id("Element4 ID")).sendKeys(string4);
        driver.findElement(By.name("submitButton")).click();
    }

Do let me know if require any further details!

This question is related to java iframe selenium

The answer is


Before you try searching for the elements within the iframe you will have to switch Selenium focus to the iframe.

Try this before searching for the elements within the iframe:

driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));

By using https://github.com/nick318/FindElementInFrames You can find webElement across all frames:

SearchByFramesFactory searchFactory = new SearchByFramesFactory(driver);
SearchByFrames searchInFrame = searchFactory.search(() -> driver.findElement(By.tagName("body")));
Optional<WebElement> elem = searchInFrame.getElem();

When using an iframe, you will first have to switch to the iframe, before selecting the elements of that iframe

You can do it using:

driver.switchTo().frame(driver.findElement(By.id("frameId")));
//do your stuff
driver.switchTo().defaultContent();

In case if your frameId is dynamic, and you only have one iframe, you can use something like:

driver.switchTo().frame(driver.findElement(By.tagName("iframe")));

On Selenium >= 3.41 (C#) the rigth syntax is:

webDriver = webDriver.SwitchTo().Frame(webDriver.FindElement(By.Name("icontent")));

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to iframe

Getting all files in directory with ajax YouTube Autoplay not working Inserting the iframe into react component How to disable auto-play for local video in iframe iframe refuses to display iFrame onload JavaScript event YouTube iframe embed - full screen Change New Google Recaptcha (v2) Width load iframe in bootstrap modal Responsive iframe using Bootstrap

Examples related to selenium

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser Class has been compiled by a more recent version of the Java Environment How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? element not interactable exception in selenium web automation Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception?