Xena
Xena

Reputation: 386

C# Selenium, how to execute javascript on web driver while page is still loading

So I have one page which takes a really long time to load due to some background stuff it loads. I want to be able to interact with it as soon as the DOM is loaded and readyState is 'interactive'. Basically I just want to click on a link to take me away from this page as I don't need for the whole thing to load before I move on. However, it seems like I can't run Javascript on the driver until the page is loaded; what happens is that the ExecuteJavascript function times out on me as well.

try
{
    element.Click(); //BrowserCommandTimeout was set to 10 seconds
}
catch (Timeout e)
{
     string state = driver.ExecuteJs("return document.readyState").ToString(); //Also times out after 10 seconds

     while ((state != "complete") && state != "interactive")
     {
          Utilities.Sleep(2000);
     }
}

Anybody have any idea how to approach fixing this problem?

Upvotes: 0

Views: 759

Answers (1)

Jonas Leão
Jonas Leão

Reputation: 1

Just this

var waitForElement = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
waitForElement.Until(driver =>_driver.ExecuteJavaScript<string>("return document.readyState").ToString() == "complete");

Upvotes: 0

Related Questions