-1

I'm trying to determine if there's specific text on the page. I'm doing this:

public static void WaitForPageToLoad(this IWebDriver driver, string textOnPage)
{
    var pageSource = driver.PageSource.ToLower();
    var timeOut = 0;

    while (timeOut < 60)
    {
        Thread.Sleep(1000);
        if (pageSource.Contains(textOnPage.ToLower()))
        {
            timeOut = 60;
        }
    }
}

The problem is that the web driver's PageSource property isn't updated after the initial load. The page I'm navigating to loads a bunch of data via JS after the page has already loaded. I don't control the site, so I'm trying to figure out a method to get the updated HTML.

ernest
  • 1,633
  • 2
  • 30
  • 48

2 Answers2

1

You are trying to solve the wrong problem. You need to wait for the text to appear using an XPath locator:

var wait = new WebDriverWait(driver);
var xpath = $"//*[contains(., '{textOnPage}')]";

wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath));
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
0

Do you really need to search entire page? I'll reference you to here: https://stackoverflow.com/a/41223770/1387701

with this code:

String Verifytext= driver.findElement(By.tagName("body")).getText().trim(); 

You can then check to see if the Verifytext contains the string you're checking for.

This works MUCH better if you can narrow the location of the text down to a particular webElement other than the body.

DMart
  • 2,401
  • 1
  • 14
  • 19