Selenium get dynamic XPath ID

I am new to Selenium, new here, and my English is not the best.

I am using selenium with .NET ...

I have an HTML page similar to this, but the number of events is 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 the data in different divs, but the count is dynamic and id (event) is also dynamic. At first I try to figure out the divs score, but this does not work. For this, I try this:

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

but this leads only to 1, and not to two examples.

when i try:

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

it prints the whole div, but when I do:

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

it prints only the first div, and I don't understand why. Can someone be so kind and give me an explanation of what is happening here and where I am going wrong?

Thanks in advance elur

+4
source share
2 answers
 decimal count = selenium.GetXpathCount("//div[@id='eventContent']"); 

This will return a div counter that has an id of eventContent - there is only one div , so you get a count of 1 (usually counts are int and not decimal s, by the way).

If you need div s counter use

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

This will count the number of eventContent of eventContent with id from eventContent . This should return 2 if required.

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

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

you will get all the text of the parent div , which naturally contains all the children of div s, but with

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

you will get the text of only the first child div . This xpath selects all children of div s, but GetText only works with one element. If you want to examine the text of each child div in turn, you first need to get the count of the child div s, and then use the 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 need a loop and manual xpath procedure (not a neater foreach ), since I believe that Selenium has no way to take xpath and return a set of elements.

+6
source

tried this but returned with 0. I solved it with a while expression, where I check with isElementPresent as follows:

 int a = 1; while (selenium.IsElementPresent("//div[@id='eventContent'][1]/div[" + a + "]")) { // check data here a++; } 

seems to work like that. Thanks so much for your help, best regards elur

0
source

All Articles