1

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;
        }
4
  • If you do a click with your script you know which element it was since the script is deterministic. Are you saying that your script has to monitor which element a real user clicks in the browser that is controlled by web driver? – Alexey R. Apr 30 at 9:50
  • I am trying to make my own test recorder - and I need to know which element was clicked on - so I can generate xpath programmatically. – Josef Širůčka Apr 30 at 10:42
  • I am not sure you're on the right way. Probably the better way is to inject a javascript to the page that would handle clicks and evaluate path for them. – Alexey R. Apr 30 at 12:22
  • I do that by injecting the javascript. And this script then returns the element - but I am trying to find out - how to identify this element then via Selenium FindElemets() – Josef Širůčka Apr 30 at 13:39
1

SOLUTION HAS BEEN FOUND:

I actually CAN use the code above, but I need to check HasCode - not the object itself:

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.GetHashCode() == objID.GetHashCode()) return c;
                c++;
            }


            return -1;
        }
1

How I can get information that I have clicked to the second WebElement from the list? Can I somehow compare the elements?

You can cross check with the text which item is clicked. Use get text from xpath. You can also compare the elements before click. You can try this.

IList<IWebElement> all = driver.FindElements(By.XPath(xpath));
int i = 0;
foreach (IWebElement element in all)
{
    if (element.Text == "#your text to be compared") 
        element.click();
    i++;    
}
1
  • Okay - And what if even the text are the same? This is not the solution... – Josef Širůčka May 12 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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