WebBrowser.Navigate does not appear to be returning DocumentText

13 views Asked by At

My code is designed to check if there is a specific item in the inventory in our database, and if so, it then uses a WebBrowser.Navigate to go to the stores website and search for that inventory item. After the navigate call, it will then do a while loop waiting for the WebBrowser ready state to not be incomplete. Once it it ready state complete, it will check if DocumentText contains the No Results Found string. However, it appears that it is not getting the string No Results Found for any of the items.

public bool TestUrl(string url, string noResultsString)
        {
            UrlData urlData = new UrlData();
            urlData.Url = url;
            urlData.NoResultsString = noResultsString;
        
            Thread t = new Thread((ParameterizedThreadStart)CheckUrlForResults);
            
            t.SetApartmentState(ApartmentState.STA);    // Use single-threaded apartment model
            t.Start(urlData);                               // Start the thread with the url

            // Wait for thread to finish
            t.Join();

            // Get the result for the thread
            bool hasResults = _ThreadResults[t.ManagedThreadId];
            return hasResults;
        }

        private void CheckUrlForResults(object data)
        {
            UrlData urlData = (UrlData)data;

            WebBrowser browser = new WebBrowser();
            browser.ScriptErrorsSuppressed = true;      // Suppress script error popups
            browser.Navigate(urlData.Url);              // Navigate to the url

            // Wait until the document is done loading
            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            // Check the document html text for the no results string
            bool hasResults = !browser.DocumentText.ToUpperInvariant().Contains(urlData.NoResultsString.ToUpperInvariant());

            // Set the thread's result value
            _ThreadResults[Thread.CurrentThread.ManagedThreadId] = hasResults;
        }

Steps I have taken:

  1. I manually enter in Chrome the URL to the store with a query string to search for that item, and if it is no longer in stock, I will get a page that says, "No Results Found".

  2. I click on a QR Code that is embedded in a marketing email, that redirects to the stores website to display the item.

  3. The code runs fine until the DocumentText portion. I have run this in remote debug and let it sit on each code of line for @5 seconds to ensure that the page has had time to load.

Even when the page has loaded, and shows No Results Found, when I copy the value of DocumentText and search for Results, it does not find it.

Any ideas or help would be greatly appreciated.

0

There are 0 answers