Selenium get dynamic ID XPath

12,949
decimal count = selenium.GetXpathCount("//div[@id='eventContent']");

This will return the count of divs that have an id of eventContent - there is only one div like this, which is why you get a count of 1 (count variables are typically ints rather than decimals, incidentally).

If you want the count of the contained divs, use

int count = selenium.GetXpathCount("//div[@id='eventContent']/div");

This will count the number of div children of the div with an id of eventContent. This should return 2, as desired.

As for your GetText examples, I think GetText will only return the text of the first node that the xpath argument selects. So with

selenium.GetText("//div[@id='eventContent'][1]")

you get the entire text of the parent div, which naturally contains all the child divs, but with

selenium.GetText("//div[@id='eventContent'][1]/div")

you get the text of only the first child div. This xpath selects all the child divs, but GetText operates on a single element only, I believe. If you want to examine the text of each child div in turn, you'll need to first get a count of the child divs, then use for loop to get each one in turn:

for(int i = 1; i <= count; ++i)
{
    string childXpath = "//div[@id='eventContent']/div[" + i + "]";
    string eventText = selenium.GetText(childXpath);

    // Processing of eventText
}

A for loop and manual xpath processing are needed here (rather than the neater foreach), as I believe Selenium doesn't have a way of taking an xpath and returning a collection of elements.

Share:
12,949
Admin
Author by

Admin

Updated on June 26, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm new on Selenium, new here and my english is not the best.

    I'm using selenium with .NET ...

    I have a HTML page like this but the number of the events are different:

    <div id="eventContent" style="text-align: center;">
           <div class="event" id="event-8971062">
                <ul>
                    <li ...></li>
                    <li ...></li>
                    <li ...></li>
                </ul>
           </div>                    
           <div class="event odd" id="event-9224880">
                <ul>
                    <li ...></li>
                    <li ...></li>
                    <li ...></li>
                </ul>
            </div>          
    </div>
    

    I need to check all datas in the different divs but the count is dynamic and the (event)id is dynamic too. I'm trying to find out the count of the divs at first but this does'nt work. For that I try this:

    DefaultSelenium selenium = new DefaultSelenium(...);
    decimal count = selenium.GetXpathCount("//div[@id='eventContent']");
    

    but this brings only 1 as result and not two for this example.

    when I try:

    Console.WriteLine(selenium.GetText("//div[@id='eventContent'][1]"));
    

    it prints all divs, but when I do:

    Console.WriteLine(selenium.GetText("//div[@id='eventContent'][1]/div"));
    

    it prints only the first div and I do not understand why. Could someone be so kind and give me an explaination of whats going on here and where I'm wrong?

    Thanks in advance elur