1

Using c# and selenium webdriver, how can I wait until the last class in a list contains a specific attribute?

In my AUT, I have three classes on a page (they are all called paragraph). I need to get the last paragraph specifically (I'm using a list but feel free to suggest better method) then wait until the last class on the page contains an outerHTML attribute of "X".

This is what I have so far:

I create a list to store all classes, get the last class and finally, get the outerHTML attribute for the last class.

IList<IwebElement> Element = driver.FindElements(By.ClassName("Paragraph"));
var GetLastElement = Element.Last();
var LastElementAttribute = GetLastElement.GetAttribute("outerHTML");

Based on my code above, how can I add a wait condition that will check the last class in the list contains an outerHTML attribute of "X"?

Banjaxx
  • 624
  • 2
  • 18
  • 34
  • 1
    You can make a wait extension as it described in that [answer](https://stackoverflow.com/a/7312740/2526001). – Sergei Iashin Dec 05 '17 at 14:57
  • Methods in post describe how to wait for an element to exist but not how to wait for an element's attribute to exist – Banjaxx Dec 05 '17 at 15:37
  • 1
    Oh, it is not so different, take a look at [this post](https://stackoverflow.com/a/15237960/2526001) and [this](https://stackoverflow.com/a/23762010/2526001). – Sergei Iashin Dec 05 '17 at 16:06

2 Answers2

2

If I understood you right - lambda for X appearance in the last Paragraph outherHTML would look like this:

Wait().Until(driver => driver
  .FindElements(By.ClassName("Paragraph"))
     .Last().GetAttribute("outerHTML")
       .Contains("X"));
unickq
  • 1,497
  • 12
  • 18
  • Thank-you that seems to work. Would you please explain the use of "driver => driver". I understand how it reads but not sure why it's needed – Banjaxx Dec 06 '17 at 15:09
  • It's just Func implementation for Until method. – unickq Dec 07 '17 at 10:25
  • Check https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/UI/ExpectedConditions.cs for better understanding – unickq Dec 07 '17 at 10:25
1

As per the defination, outerHTML returns the html of the element and its child elements. In certain cases it can be set to completely replace an element with an html string. Hence in this case it would be worth looking into any of the following ExpectedConditions :

  • TextToBePresentInElement
  • TextToBePresentInElementLocated
  • TextToBePresentInElementValue(IWebElement, String)
  • ToString

Among the list of the ExpectedConditions mentioned, ToString looks more promising as per your case is concerned as it Returns a String that represents the current Object. Hence we can easily search the String for the intended character "X"

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352