4

Greetings Stack Overflow Community,

I have a quick question that, when answered, will help anyone else that is looking to use Selenium and C# to wait until text appears on a web page that is not a clickable link but that is shown within the label tag.

I have the following example code,

<div class="class1" style="style7">
<label id="statusBar" class="class2">Not Ready</label>
</div>

Using Selenium and C# I am trying to insert a wait until an expected condition that will pause the code and then resume once the condition is satisfied.

I can find the label and perform an action once it is found by XPath...

wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='statusBar']")));

But, when I try waiting for the "Not Ready" text to be visible within that label tag, the C# script is stuck at this point and can't find the text within the label.

This is what I have tried based upon several stack overflow articles that don't address this question.

wait.Until(ExpectedConditions.TextToBePresentInElementLocated(By.Id("statusBar"), "Not Ready"));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='statusBar']//label[contains(text(), 'Not Ready')]")));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='statusBar'][contains(text(), 'Not Ready')]")));

Any assistance would be greatly appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sethjbr
  • 125
  • 2
  • 11
  • 1
    Are you sure that it ignores the wait (using TextToBePresent...)? How long is it before the text appears? – Andy G Nov 01 '18 at 17:34
  • It is at least a few seconds, but this wait comes after a previous wait is satisfied. When that first wait is satisfied, this second wait text is also present at the same time. Therefore, it should be almost instantaneous. I will try inserting a sleep though and see if it makes any difference. – sethjbr Nov 01 '18 at 17:39
  • with a short google search i found this: https://stackoverflow.com/a/49867605/8179099 – Moshe Slavin Nov 01 '18 at 17:42
  • @AndyG I just tested it with a Thread.Sleep of 10 seconds and it still did not find the "Not Ready" text within the label tag with each of these three methods of finding that text within the label tag. – sethjbr Nov 01 '18 at 17:48
  • @MosheSlavin Thanks for the article, however that doesn't apply to this situation as the expected conditions is not going to be obsolete as visual studio is not presenting that warning anymore. – sethjbr Nov 01 '18 at 17:52
  • Check and make sure that there isn't more than one element on the page with the ID = "statusBar". I'm wondering if there isn't more than one and the one you want is not the first so it's never found. You can use `$$("#statusBar")` in the console. – JeffC Nov 01 '18 at 20:50

1 Answers1

2

Seems you were pretty close. You need to induce WebDriverWait and you can use either of the following solutions:

  • Using TextToBePresentInElementLocated():

    • CssSelector:

      wait.Until(ExpectedConditions.TextToBePresentInElementLocated(By.CssSelector("label.class2#statusBar"), "Not Ready"));
      
    • XPath:

      wait.Until(ExpectedConditions.TextToBePresentInElementLocated(By.XPath("//label[@class='class2' and @id='statusBar']"), "Not Ready"));
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352