Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindElement doesnt iterate when iterating elements in IReadOnlyCollection

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);
}
like image 211
Marek Avatar asked Oct 19 '22 11:10

Marek


1 Answers

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

like image 115
Jcl Avatar answered Oct 31 '22 10:10

Jcl