简体   繁体   中英

Selenium WebDriver can't locate element in iframe even when switching to it

I have searched a lot of forum topîcs to solve my issue but I didn't find any similar problem. I'm writing tests with Selenium WebDriver using Java.

The website I'm testing uses an iframe where all the visible content for the user will be loaded into. It looks like something like this :

<html>
  <head>
     (...)  
  </head>
  <body>
    <iframe id="portalIframe">  
      <html>
        <body>
          <h2 id="aui_3_2_0_1594">Etat des agents</h2>
          ...
        </body>
      </html>       
    </iframe>       
  </body>
</html>

My problem is the following : finding an element in the iframe doesn't always work. When I want to locate such an element I first run a method to switch to the iframe :

protected void switchIframe() {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    By by = By.id("portalIframe");
    try {
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by));
} catch (TimeoutException e) {
    e.printStackTrace();
    }
}

And then I can for example find a h2 element:

 driver.findElement(By.xpath("//h2[contains(text(),'Some text')]"));

What I can't understand is that sometimes selenium doesn't manage to find elements on the webpage. For instance I just managed to find the h2 element, then I click on a menu link to load another page but when I try to find again a < h2 > element on the new page, I get a "element not found error" and if I try to add switchIframe() before that, selenium says it cannot find the iframe.

It seems to happen "randomly" so I don't really know what to do to solve this issue. The issue is the same on ChromeDriver and FirefoxDriver. I don't use others drivers because only Firefox and Chrome will be used for this private website.

EDIT I'm sorry I can't paste the html content because it's a intranet website and I can't share it here. An example of a h2 element I try to locate < h2 id="aui_3_2_0_1594">Etat des agents< /h2 >

Also my problem is quite similar to this issue : Selenium WebDriver not able to find elements which are not present in the Page Source but present in HTML when seen through Developer Tools The issue appens not only on h2 elements but also for example on "a" elements I want to click on.

Thanks

It appeared that I wasn't waiting enough time when looking for html elements. I just increased the timeout of my explicit wait method. I'm not talking about switchIframe() but I'm referring to another method used to wait for elements to be present :

So, I've increased the timeout from 3 to 6 seconds and it seems to work great now.

protected WebElement waitForElementVisible(By by) {
    WebDriverWait wait = new WebDriverWait(driver,6);
    WebElement element = null;

    try {
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (TimeoutException e) {
        //method logging an error
        error("Timeout : element " + by.toString() + " is not visible");
    }
    return element;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM