Throw "No such element" after perform switch to parent iFrame

Not reproducible Issue #14556985


Truc N.
Nov 7, 2017
This issue is public.
Found in
  • Microsoft Edge
Reported by 1 person

Sign in to watch or report this issue.

Steps to reproduce

I’m using selenium and I’d like find a web element in a iframe but failed. For further detail please run java code below:

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.List;

public class TestClickFF {
public static void main(String [ ] args) throws Exception{
// // EDGE
System.setProperty("webdriver.edge.driver", “D:\selenium\web driver\edge\MicrosoftWebDriver_15063.exe”);
DesiredCapabilities edgeCapabilities = DesiredCapabilities.edge();
WebDriver webDriver = new EdgeDriver(edgeCapabilities);
// System.setProperty("", “D:\selenium\web driver\chrome\chromedriver_win32_2.33.exe”);
// ChromeOptions chromeOptions = new ChromeOptions();
// WebDriver webDriver = new ChromeDriver(chromeOptions);

    // goto site
    // get the element on iframe
    WebElement webElement = null;
    try {
        webElement = webDriver.findElement(By.xpath("//input[@type='button']"));
    } catch (Exception e) {
    if (webElement == null) {
        String urlCurrent = webDriver.getCurrentUrl();
        List<WebElement> webElementList = findElementInWholePage(webDriver, urlCurrent);
        if (webElementList != null &amp;&amp; webElementList.size() > 0) {
            webElement = webElementList.get(0);
    // click element but it not perform "onclick" event that set on element
    if (webElement != null) {;
    } else {
        System.err.println("Element not found.");

private static List<WebElement> findElementInWholePage(WebDriver wdDriver, String urlFindStart) throws InterruptedException {
    List<WebElement> lsEle = null;
    try {
        // Get all FRAMES from selected page
        List<WebElement> lsIFrames = wdDriver.findElements(By.xpath("//iframe"));

        boolean isSwitchedToIframe = false;
        // Iterate through FRAMES (recursive loop)
        for (WebElement weIFrame : lsIFrames) {
            if (lsEle == null || lsEle.size() == 0) {
                try {
                    isSwitchedToIframe = false;
                    if (!weIFrame.isDisplayed() || weIFrame.getSize().getWidth() <= 1 || weIFrame.getSize().getHeight() <= 1) {
                    isSwitchedToIframe = true;
                } catch (WebDriverException e) {
                } catch (Exception ex) {
                    System.out.printf("Exception: " + ex);
                // Search element by XPATH
                lsEle = wdDriver.findElements(By.xpath("//input[@type='button']"));
                if (!lsEle.isEmpty()) {
                    return lsEle;
                } else {
                    // Perform search on next FRAME
                    lsEle = findElementInWholePage(wdDriver, urlFindStart);
                    if (isSwitchedToIframe) {
                        switchToParentFrame(wdDriver, urlFindStart);
            } else {
                return lsEle;
    } catch (StaleElementReferenceException e) {
    return lsEle;

// Back to parent frame
private static void switchToParentFrame(WebDriver webDriver, String urlFindStart) {
    try {
        String urlCurrent = webDriver.getCurrentUrl();
        if (urlCurrent.equals(urlFindStart)) {
    } catch (Exception e) {



0 attachments

    Comments and activity

    • Hi,
      Can you please help to investigate and let me know if any required.
      Thank you so muck.

    • Microsoft Edge Team

      Changed Assigned To to “Steven K.”

    • Hi Truc,

      Yes, I will take a look in the next couple days.


    • Hi Truc,

      I looked at the code sample you provided and have a some comments and questions.  Let me know if you have already sorted out this issue as well.

      First question, does this code run in any other browser?  I ask because this is not what I would call a simplified repro.

      A couple comments.  First, I think your switchToParentFrame() method can be removed and replaced with the single line “driver.switchTo().parentFrame();”

      I appears you are using the non-iframe url as the test parameter for switching frames.  It seems doing this will cause the switchToParentFrame() to have the incorrect url to test against.  I do not see this variable getting updated which might explain the error I am seeing, which is a "Stale element reference".  This means the element was found but is no longer valid as it is gone or has been updated.

      "Session ID: 4C198CF5-B25B-4E75-B476-665EFF0C230B Stale element reference (WARNING: The server did not provide any stacktrace information)"

      Also, I tested basic iframe traversal and element/button selection and did not have any errors.  I did not have a recursive find for all nested iframes though.  :)

      Perhaps a simpler approach is not possible on that specific page and you wanted a very general solution.  If a simpler approach could work, here is an example of going down the iframe nesting and popping out to the main page using driver.SwitchTo().DefaultContent();

      Hope this helps.


    • Microsoft Edge Team

      Changed Status to “Not reproducible”

      Changed Assigned To to “James M.”

    You need to sign in to your Microsoft account to add a comment.

    Sign in