Reputation: 644
I'm trying to assert that specific UL tags within my webpage contain certain text. I'm not sure how to get for example, the third UL tag within a class and then assert it's text. Here is an example of the HTML code from my webpage:
<td class="def"><ul>
<li>1 -- test 1</li>
<li>2 -- test 2</li>
<li>3 -- test 3</li>
<li>4 -- test four</li>
</ul></td>
I'm taking a different (and likely naive) approach to get this working but it's not really ideal for me - and I'm only doing it this way because I'm not sure how to get it working the way I would like. I'm asserting that the length of text from the outer html of the "def" class contains a specific count:
(from memory)
String Def = utility.driver.findelement(By.ClassName("def").GetAttribute.("outerHTML");
int Length = Def.Length;
Assert.IsTrue(Length.equals("36"));
Upvotes: 0
Views: 1890
Reputation: 25076
You can either directly find that element using an XPath selector that matches on text (example selector below, not tested):
//td[contains(@class, 'def')]//ul/li[text()='4 -- test four')]
(find me a td
that contains a class
of def
, get the ul
underneath it, and get the li
underneath that which also has a text
of 4 -- test four
)
You can also harness the power of LINQ in C# to do some leg work for you. The thing you are missing is finding child elements of a given element. This is done by chaining .FindElement
commands:
Driver.FindElement(By.ClassName("def")).FindElement(By.TagName("ul"));
The above would get the first ul
element that is a direct child of the td
element in your example. WebDriver will be able to figure out the child & parent relationships for you.
So to put this into your situation a bit better, here's how you would do it.
Driver.FindElement(By.ClassName("def"));
Get the table
we want.
Driver.FindElement(By.ClassName("def")).FindElement(By.TagName("ul"));
Get the ul
we want that's a direct child of that table
.
Driver.FindElement(By.ClassName("def")).FindElement(By.TagName("ul")).FindElements(By.TagName("li"));
Get the li
elements of that ul
. Note the FindElements
(extra s!)
Driver.FindElement(By.ClassName("def")).FindElement(By.TagName("ul")).FindElements(By.TagName("li")).First(i => i.Text.Equals("4 -- test four"));
Since it's an IList
that returns, LINQ can then take it and return the first one that has a .Text
property equal to what you need.
Upvotes: 1