如何让Selenium 3.4.0等待页面加载?含响应时间计算等相关问题
Hey there, let's work through your Selenium 3.4.0 problems step by step— I’ve dealt with similar quirks in older versions, so I know how frustrating it can be when your go-to code stops working. Here’s what you can try:
1. Calculating Page Load Time or Waiting for Page Completion
If your old WebDriverWait approach isn’t cutting it, here are two reliable alternatives:
Track load time with timestamps
This straightforward method records the time before and after navigating to a page to calculate total load time:long startTime = System.currentTimeMillis(); driver.get("your-target-url"); long totalLoadTime = System.currentTimeMillis() - startTime; System.out.println("Page loaded in: " + totalLoadTime + " ms");Wait for
document.readyStatevia JavaScript
UseJavascriptExecutorto wait until the browser reports the page is fully loaded. This works well for single-page apps or pages with dynamic content:WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete") );Note: Make sure you’re importing
org.openqa.selenium.support.ui.WebDriverWaitandorg.openqa.selenium.JavascriptExecutorcorrectly in your Eclipse project.
2. Detecting Modal Dialogs After Button Clicks
Modals are often loaded dynamically, so you’ll need to wait for their core elements to become visible. Here’s how to handle this:
- First, trigger the button click:
driver.findElement(By.id("your-button-id")).click(); - Then, wait for the modal’s key element (like its container ID or class) to appear. If
ExpectedConditionsisn’t working, use a custom wait condition:
If your modal is inside an iframe, don’t forget to switch to the iframe first withWebDriverWait wait = new WebDriverWait(driver, 10); WebElement modalContainer = wait.until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver driver) { return driver.findElement(By.id("modal-container-id")); } }); // Verify the modal is visible if (modalContainer.isDisplayed()) { System.out.println("Modal dialog has loaded successfully"); }driver.switchTo().frame("iframe-id")before locating the modal element.
3. Waiting for Elements to Be Visible When Wait.until() Fails
First, double-check that you’re importing the correct classes: org.openqa.selenium.support.ui.WebDriverWait and org.openqa.selenium.support.ui.ExpectedConditions (this is a common mistake!). If that’s not the issue, try these workarounds:
Custom explicit wait implementation
Build your own wait loop to poll for the element until it’s visible:public WebElement waitForElementVisible(By locator, int timeoutInSeconds) { WebElement targetElement = null; long endTime = System.currentTimeMillis() + (timeoutInSeconds * 1000); while (System.currentTimeMillis() < endTime) { try { targetElement = driver.findElement(locator); if (targetElement.isDisplayed()) { break; // Exit loop once element is visible } } catch (NoSuchElementException | StaleElementReferenceException e) { // Ignore temporary exceptions and keep waiting } // Pause briefly between checks to avoid overwhelming the browser try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } return targetElement; } // Usage example WebElement myElement = waitForElementVisible(By.id("myId"), 10);Use FluentWait for more control
FluentWaitlets you customize polling intervals and ignore specific exceptions, which can be more robust thanWebDriverWaitin some cases:import org.openqa.selenium.support.ui.FluentWait; import java.util.concurrent.TimeUnit; import org.openqa.selenium.NoSuchElementException; Wait<WebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(10, TimeUnit.SECONDS) .pollingEvery(500, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); WebElement visibleElement = fluentWait.until(driver -> driver.findElement(By.id("myId")));
Also, ensure your Eclipse dynamic web project has all the required Selenium JARs— specifically selenium-support.jar (which contains ExpectedConditions and WebDriverWait) matching version 3.4.0.
内容的提问来源于stack exchange,提问作者sherin shaf

