Selenium ChromeDriver C # how to navigate the page before loading it?

I am testing data flow for a client website. It has advertisements that take significantly longer than the items of each page that I would like to check with Selenium commands.

I have no control over the ads and cannot turn them off.

I would like to navigate through each page with clicks until the page is fully loaded. I know this is possible because I can do it manually with the mouse. However, despite my attempts, the stubborn chromeDriver won't start automating until the entire page has loaded.

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

Also, I am using the recommended Selenium Page Object Model. I have implemented WaitForLoad () like this:

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

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

      

PageRegex above will work, but only after full page load. This is disappointing because I can visually see that the text string intended for parsing is meant to be viewed on screen. This leads me to think there are settings elsewhere, perhaps when I tweak the ChromeDriver that would allow me to parse the Session.Driver.PageSource before it is fully loaded.

This is how I start 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;

      

+3


source to share


3 answers


To interact with the page before loading it, you can either decrease the timeout or 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 pending by setting your page load strategy 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")));

      

+3


source


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



WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
            wdw.Until(ExpectedConditions.ElementToBeClickable(By.Id("ElementId")));

      

+1


source


I would like to navigate through each page with clicks until the page has finished loading

Am I getting it right that you want to click on some links while the page is still loading? If you know what elements you are going to click, perhaps you can do the same as in this question How to click an element before the page is fully loaded, I am stuck, it will take too long to complete loading

0


source







All Articles