简体   繁体   中英

Selenium WebDriver get(url) speed issue

The get(url) method waits for the web page to be fully loaded. If the page has a lot of stuff on it, loading can be very slow.

Is there a way to navigate to the target web page and wait only for the WebElement of interest? (ie not the banners, ads, etc.)

Thanks!

You can use Page load timeout . As far as I know, this is definitely supported by FirefoxDriver and InternetExplorerDriver , though I'm not sure about other drivers.

driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.MILLISECONDS);
try {
    driver.get("http://google.com");
} catch (TimeoutException ignored) {
    // expected, ok
}

Or you can do a nonblocking page load with JavaScript:

private JavascriptExecutor js;

// I like to do this right after driver is instantiated
if (driver instanceof JavascriptExecutor) {
    js = (JavascriptExecutor)driver;
}

// later, in the test, instead of driver.get("http://google.com");
js.executeScript("window.location.href = 'http://google.com'");

Both these examples load Google, but they return the control over the driver instance back to you immediatelly instead of waiting for the whole page to load. You can then simply wait for the one element you're looking for.


In case you didn't want this functionality just on the WebDriver#get() , but on you wanted a nonblocking click() , too, you can do one of these:

  1. Use the page load timeout as shown above.
  2. Use The Advanced User Interactions API ( JavaDocs )

     WebElement element = driver.findElement(By.whatever("anything")); new Actions(driver).click(element).perform(); 
  3. Use JavaScript again:

     WebElement element = driver.findElement(By.whatever("anything")); js.executeScript("arguments[0].click()", 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