0

I want to create a method in my selenium test, which does wait until an element with a specific string inside the InnerHTML is displayed.

Currently I have this here:

string textToCompare = "Hello";
w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath(PathElement))).GetAttribute("innerHTML");

In the codeline above, im just waiting for PathElement and get the attribute of the innerHTML.

But i want this to behave like: Wait for the element which has the textToCompare string inside of its innerHTML

If there are other waiting methods for selenium, i would be open for any suggestions.

Or is there a way to navigate with the PathElement variable directly to the InnerHTML?

Here is the inspect of the webpage: enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Beardy
  • 163
  • 1
  • 15
  • try fluent wait – Dilip Meghwal Dec 22 '21 at 12:03
  • How is _`PathElement`_ defined? – undetected Selenium Dec 22 '21 at 12:03
  • PathElement is just a simple XPath. In this case `string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4']"`. Our website is using mudblazor, which creates elements dynamically. So thats why the naming of the element is weird and has not a specific ID. – Beardy Dec 22 '21 at 12:05
  • I added a screenshot of the `inspect`. The red circles is the text i want to wait for using the waitmethod. I know that i can easily wait just for the `PathElement`, but i want to wait for a specific string inside innerHTML – Beardy Dec 22 '21 at 12:09

1 Answers1

0

To wait until an element with a specific string as the InnerHTML is displayed instead of ElementExists() you need to induce WebDriverWait for the ElementIsVisible() and you can use either of the following Locator Strategies:

  • Using ElementIsVisible() and Text attribute:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4']"
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
  • Using ElementIsVisible() and starts-with specific text:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4' and starts-with(., 'Importiere')]"   
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
  • Using ElementIsVisible() and containing specific text:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4' and contains(., 'Importiere')]"  
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
  • Using ElementIsVisible() and generic text:

    string PathElement = "//p[@class='mud-typography mud-typography-body1 mud-surface-text mb-4' and text()]" 
    w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(PathElement))).Text;
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352