FindElement does not iterate when iterating elements in IReadOnlyCollection

I iterate over elements that were taken from HTML.

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

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); } 
+5
source share
1 answer

This is because you use // in Xpath , which means "select the descendants of root", so it returns the first one that it finds in the document

If you want to find a descendant of this node, you can try:

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

.// means "select node context descendants"

You can check all abbreviations for axes here

+3
source

Source: https://habr.com/ru/post/1247027/


All Articles