0

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

Community
  • 1
  • 1
singe3
  • 2,065
  • 4
  • 30
  • 48
  • can you share the relevant code inside of the iframe that you are attempting to interact with? – ddavison Jun 20 '14 at 14:37
  • A h2 element like that : < h2 id="aui_3_2_0_1594">Etat des agents< /h2 > Also, the id attribute may not be used as a reliable locator because it changes each time the page reloads. – singe3 Jun 20 '14 at 14:44
  • i've edited your question based on your feedback. Is this correct? you want to select this h2? – ddavison Jun 20 '14 at 14:51
  • yes it is correct thank you , though the h2 element I try to find is included in a hierarchy of several div blocks but that doesn't matter – singe3 Jun 20 '14 at 14:54

1 Answers1

1

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;
}
kenorb
  • 155,785
  • 88
  • 678
  • 743
singe3
  • 2,065
  • 4
  • 30
  • 48