61

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!

1

5 Answers 5

100

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")));
Sign up to request clarification or add additional context in comments.

6 Comments

As suggested, added below...but I am stuck with same issue yet public static void called_funciton(String string1, String string2, String string3, String string4) { driver.switchTo().frame(driver.findElement(By.name("iFrameTitle"))); 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(); }
Try putting the new switchTo line as the very first line in the code block. I.e. before WebDriverWait wait_iframe before you do anything else.
Thank you, this time I am able to move a step further, however stuck with an exception: Exception in thread "main" java.lang.NullPointerException at roll_forward_test.account_creation.thistle_home(xyz_creation.java:680) at roll_forward_test.account_creation.main(xyz_creation.java:463) the line (xyz_creation.java:680) refers to driver.findElement(By.name("Element1 Name")).sendKeys(string1); on the called function and the line (xyz_creation.java:463) refers to the calling function. calling_function(S1, S2, S3, S4); please advice!
If your original question about accessing elements in iframes has been answered, then please mark this answer as accepted and we can look at the subsequent issues.
I spent hours trying to find a solution, you put me on the right path. Thank you!
|
69

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")));

1 Comment

+1 for the switchTo().defaultContent() i'm guessing that's how you unselect the frame after your work is done.
4

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

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

1 Comment

this is a Java question!
1

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();

Comments

1

For python users:

driver.switch_to.frame(driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div[1]/div[6]/div[2]/div/iframe"))

And also switch back:

driver.switch_to.default_content()

1 Comment

This may work when using python, but since OP asked expressly about Java it is not really an answer to their question...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.