Selenium get dynamic ID XPath
I'm new on Selenium, new here and my english is not the best开发者_C百科.
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
decimal count = selenium.GetXpathCount("//div[@id='eventContent']");
This will return the count of div
s 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 int
s rather than decimal
s, incidentally).
If you want the count of the contained div
s, 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 div
s, 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 div
s, 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 div
s, 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.
tryed this but returns with 0. I solved this with a while expression where I check with isElementPresent like this:
int a = 1;
while (selenium.IsElementPresent("//div[@id='eventContent'][1]/div[" + a + "]"))
{
// check data here
a++;
}
seems to work so. thanks a lot for your help, best regards elur
精彩评论