Arif YILMAZ
Arif YILMAZ

Reputation: 5876

How to iterate sub elements of <ul> in Selenium with C#

I am trying to iterate <li> items of <ul> object in Selenium with C#. I am very new to it and trying to find a easy way write a code for my test project.

here is my sample html code, I would like to access those links below...

<ul class="list-menu">
    <li>
        <ul>
           <li><a class="head" href="/seramik-banyo-urunleri">Seramik Banyo &#220;r&#252;nleri</a></li>
           <li><a href="/seramik-banyo-urunleri/lavabo">Lavabo</a></li>
           <li><a href="/seramik-banyo-urunleri/klozet">Klozet</a></li>

my c# code is like below

IList<IWebElement> results = driver.FindElements(By.XPath("//div[@class='list-menu']/li/lu/li"));

but it doesn't get the links. what can I do to fix this?

Upvotes: 0

Views: 2548

Answers (1)

alecxe
alecxe

Reputation: 474031

You have a typo in the XPath expression, replace:

//div[@class='list-menu']/li/lu/li

with:

//div[@class='list-menu']/li/ul/li

Or, you can use a more compact CSS selector instead:

driver.FindElements(By.CssSelector(".list-menu > li > ul > li"));

where > means a direct parent-child relationship.

Upvotes: 3

Related Questions