Search code examples
c#selenium-webdriverphantomjs

How to stop loading page in Selenium WebDriver when an element is already present?


I'm scraping a website, which for some reason takes 15 seconds to load, but the elements I need get loaded in the first 5 seconds.

The question is, can I stop loading a page, and move on with the code when the code is already present?

Current code:

driver.Navigate().GoToUrl(url);

new WebDriverWait(driver, TimeSpan.FromSeconds(20))
    .Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath)));

Current behaviour:

  1. I call driver.Navigate().GoToUrl(url);
  2. The driver starts loading the page
  3. The element is present within approx. 5 seconds (that's when I want to move on to the next statement)
  4. The driver waits for the whole 15 seconds to fully load the page
  5. Then the driver finally moves to the WebDriverWait function

The problem:

All of the solutions in similar questions are called after the page fully loaded, making the answer useless.

Can I do something about it? Thank you.


Solution

  • Method .Navigate().GoToUrl is created such a way that next line is executed only when page gets fully loaded. so yes, it doesn't matter what you are writing in code, after navigate method, it will never work until the page gets fully loaded. As a workaround you can opt for timeout option, with this it will throw a exception as page remains to load so we should catch that and execute our next code.

    //This is java code, so please pick only the logic from here:
    // Set the page load timeout to 10 seconds.

    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  
    
    try {  
      driver.Navigate().GoToUrl(url);  
    } catch (TimeoutException e) {  
      // Ignore the exception.  
    }