I have a problem and I don't know how to solve that...
I have 5 exactly the same elements on the page (same div, same class, no id, same inner text). So I can build the XPath - where Selenium returns to me - the IList with IWebElements.
My Goal is to iterate thru this list and "somehow" get the IWebElement that I have clicked on...
I know, that I can use XPath like this: //div[2] for the second one, but this is the thing - How I can get information that I have clicked to the second WebElement from the list? Can I somehow compare the elements? (I have tried to compare GetHash() and compare the webelements - but both did not work)
I would like to have something like this:
- User clicks to the element
- My code determines, that there are many of them
- My code get the information that user clicked to the n-th element from the list
- My code will generate xpath -> //div[x] where x is the order of the element in the list
Any advice?
I have tried this but it does not work:
private int GetTheOrderOfTheElement(string xpath, IWebElement objID)
{
IList<IWebElement> el = driver.FindElements(By.XPath(xpath));
int c = 0;
foreach(IWebElement e in el)
{
if (e == objID) return c;
c++;
}
return -1;
}