Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium ChromeDriver C# how to navigate page before it loads?

I am testing data flow for a client's website. It has advertisements that take substantially longer to load than the data elements of each page that I would like to test with Selenium commands.

I don't have control over the ads and can't silence them.

I would like to navigate each page with clicks prior to the complete page loading. I know that this is possible because I can do it manually using the mouse. However, despite my attempts the stubborn chromeDriver will not begin automation until the entire page is loaded.

I am using C# .Net 4.6.1, chrome32_55.0.2883.75, and Selenium version 3.0.1.0.

Further, I am using the recommended Selenium page object model. I implemented WaitForLoad() like this:

    public override void WaitForLoad()
    {
        _isLoaded = Wait.Until(d =>
        {
            lock (d)
            {
                SwitchToSelf();

                return PageRegex.IsMatch(Session.Driver.PageSource);                    
            }
        });
    } 

The PageRegex above will work but only after the full page is loaded. Which is frustrating because I can visually see that the text string that the PageRegex is designed to parse is on the screen. This leads me to believe that there is a setting elsewhere, perhaps while I am configuring ChromeDriver, that would enable me to parse the Session.Driver.PageSource prior to it being completely loaded.

This is how I am instancing the ChromeDriver:

            var options = new ChromeOptions();

            options.AddArguments("test-type");


            options.AddArgument("incognito"); // works
            options.AddArgument("--disable-bundled-ppapi-flash"); // works! this turns off shockwave
            options.AddArgument("--disable-extensions"); // works
            options.AddArguments("--start-fullscreen");

            string workFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
                                                 "\\SHARED";

            options.BinaryLocation = workFolder + @"\Chrome32\chrome32_55.0.2883.75\chrome.exe";
            var driver = new ChromeDriver(options);
            driver.Manage().Cookies.DeleteAllCookies();
            return driver;
like image 662
sapbucket Avatar asked Feb 04 '23 17:02

sapbucket


2 Answers

To interact with the page before it has finished loading, you can either lower the timeout and catch the exception:

IWebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromMilliseconds(500));

try {
    driver.Navigate().GoToUrl("http://www.deelay.me/5000/http://www.deelay.me/");
} catch (OpenQA.Selenium.WebDriverTimeoutException) { }

// waits for an element 
var body = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("body")));

Or you can disable the waiting by setting the page load stategy to none :

var options = new ChromeOptions();
options.AddAdditionalCapability("pageLoadStrategy", "none", true);

IWebDriver driver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

driver.Navigate().GoToUrl("http://www.deelay.me/5000/http://www.deelay.me/");

// waits for an element 
var body = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("body")));
like image 122
Florent B. Avatar answered Feb 07 '23 07:02

Florent B.


Have you tried with ElementToBeClickable or VisibilityOfElementLocated methods of ExpectedConditions class. I think it should work in the scenario you mentioned.

WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
            wdw.Until(ExpectedConditions.ElementToBeClickable(By.Id("ElementId")));
like image 23
Nitin Sahu Avatar answered Feb 07 '23 07:02

Nitin Sahu