5

I am iterating over elements which were taken from HTML.

structureElement seems to hold found element, but when I run .FindElement() on it, it returns always value from first iteration. Why is that?

Here is my code:

IReadOnlyCollection<IWebElement> structureElements =
    driver.FindElements(By.XPath(".//div[contains(@id, 'PSBTree_x-auto-')]"));
//.Where(sE => sE.FindElement(By.XPath("//img[@title]")).GetAttribute("title") != "Customer Item")
IWebElement tmp;
foreach (IWebElement structureElement in structureElements)
{
    //if (structureElement.FindElement(By.XPath("//span//img[@title]")).GetAttribute("title").Contains("Customer Item"))
    //    continue;
    tmp = structureElement.FindElement(By.XPath("//span[@class='cat-icons-tree']/img"));
    ReportProgress(progress, ref r, ct, allsteps, message + tmp.GetCssValue("background"));
    Thread.Sleep(100);
    ReportProgress(progress, ref r, ct, allsteps, message + structureElement.Text);
    Thread.Sleep(300);
}

1 Answer 1

3

That's because you are using // in the Xpath, which means, "select descendants of root", so it's returning the first it finds in the document

If you want to find a descendant of that node, you may want to try:

.//span[@class='cat-icons-tree']/img

.// means "select descendants of the context node"

You can check all the abbreviations for the axes here

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.