2
var value = selenium.GetXpathCount("//div[contains(@id,'spnPriceDetails')]");
int clickNo = 1;
for (var j = 1; j <= value; j++)
{
    if (clickNo == j)
    {

        selenium.Click("//div[contains(@id,'spnPriceDetails')]");
    }
    clickNo = clickNo + 1;
}

I have 25 same links on one page I have Identify total number of links using Xpath Count. I Click on First Link But when i try to click on second and thirld link it click on first link every time instead of second and thirld

2 Answers 2

1

Your code isn't specifying a later link, it's doing the same search each time, retrieving all 25 that match "//div[contains(@id,'spnPriceDetails')]" and then clicking the first one in the resulting set of matches. You need to add the iterator variable into the search string, like so:

for (var j = 1; j <= value; j++)
{
   selenium.Click("(//div[contains(@id,'spnPriceDetails')])[" + j + "]");
}

This way it will click through each value in the list of matches.

Note: I can't remember if xpath will start with 0 index or 1-index. It's supposed to be 1, but if it isn't you may need to start your loop at 0 instead.

Sign up to request clarification or add additional context in comments.

8 Comments

I figured this was the issue, but had a hell of a time finding details in the Selenium documentation.
Tarwn Thanks for ur reply,but still my problems doesnot get sought out..For that particular element we have id generated i.eid="spnPriceDetails30-IRCMPTZ" out of that spnPriceDetails is static and other part is dynamic.I found out number of element using that id i.e 25,but when try to click on second element it always click on first and i dont take that whole id because every time we search we get different result and different dynamic id.. so i am looking in to some indexing like of concept same way we are using in QTP,but here we have Xpath,CSS in selenium and its not working with div....
So if you add the index to your search string like I show in the post above, does it continue to click the first link every time?
when i try using the above you have suggested i get:ERROR: Invalid xpath [2]: //div[contains(@id,'pnlMinMaxPrice')])[1],in my case neither position()=something works nor the above you have suggested works...:(:( and because of this i have stuck from last 2 days...
Is it stripping the beginning parenthesis off of the xpath statement or did you miss it when copying? it should be: (//div[contains(@id,'pnlMinMaxPrice')])[1]
|
1

I would try to use Position in the xpath expression. I have created a fluent Xpath library that might be helpful:

The code would be something like:

for (int i = 1; i <= value; i++)
{
    selenium.Click(XPathFinder.Find.Tag("div").With.Attribute("id").
    Containing("spnPriceDetails").And.Position(i).ToXPathExpression());
}

Some more info on the XPath library can be found here: http://www.unit-testing.net/CurrentArticle/How-To-Write-XPath-for-Selenium-Tests.html

I guess the plain xpath string way would be:

for (int i = 1; i <= value; i++)
{
    selenium.Click(string.Format("//div[contains(@id,'spnPriceDetails') and position()={0}]",i));     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.