user1296259
user1296259

Reputation: 521

Issue waiting for element to load using selenium in Java

I am trying to use Selenium to find a webElement on a page that is loaded using ajax and/or jquery.

It seems no matter what I do, the element is never found.

I am using Firefox 20, and Selenium 2.31.0.

That section that I am trying to get has html that looks like this (I got this using the Inspector Tool in firefox):

 <form id="file_upload" method="post" enctype="multipart/form-data" action="myAction.php">
    <table class="table table-striped">
      <tbody>
        <tr>
          <td>
            <input class="input-file" type="file" name="file"></input>
            <input class="btn btn-primary" type="submit" value="upload"></input>
          </td>
          <td style="text-align:right;"></td>
        </tr>
      </tbody>
    </table>
  </form>

I have tried:

WebDriverWait driverWait = new WebDriverWait(driver, 10000);

WebElement dynamicElement =  driverWait.until(ExpectedConditions.presenceOfElementLocated(By.id("file_upload")));

System.out.println(dynamicElement.getText());

and I have tried:

WebElement dynamicElement = new FluentWait<WebDriver>(driver).withTimeout(1,  TimeUnit.MINUTES).pollingEvery(1, TimeUnit.SECONDS)
    .ignoring(NoSuchElementException.class).until(ExpectedConditions.visibilityOfElementLocated(By.id("file_upload")));

System.out.println(dynamicElement.getText());

But anything I try never finds it, or times out no matter how much time I give it to find that element. Any help would be much appreciated. Thankyou.

Upvotes: 0

Views: 2362

Answers (2)

Yi Zeng
Yi Zeng

Reputation: 32855

// to make sure you are in the default content
// this is not necessary in the most of the cases
driver.switchTo().defaultContent(); // use only if you are in a frame already

driver.switchto().frame("framename or id");
//driver.switchto().frame(driver.findElement(By.xpath("frame locator")));
//driver.switchto().frame(zero-based frameindex);

// now in the frame
WebDriverWait driverWait = new WebDriverWait(driver, 10000);
WebElement dynamicElement = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.id("file_upload")));
System.out.println(dynamicElement.getText());

// if you want to get out of all frames back to default content
driver.switchTo().defaultContent();

Upvotes: 1

John Landis
John Landis

Reputation: 26

Try upgrading Selenium. Not sure if its the only issue but FF 20 is supported by Selenium 2.32.0.

Selenium History - Notes at bottom of page show FF 20 support.

Upvotes: 0

Related Questions