Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for label tag text in Selenium web driver in C#

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.

like image 870
sethjbr Avatar asked Oct 15 '25 20:10

sethjbr


1 Answers

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"));
      
like image 104
undetected Selenium Avatar answered Oct 18 '25 12:10

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!