简体   繁体   中英

SELENIUM - How to count IWebElement in IList<IWebElement> and get the position in the list

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 which 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 second WebElement from the list? Can I somehow compare the elements? (I have tried compare GetHas() and compare the webelements - but both did not worked)

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

Lets see the real example - I ve got this HTML:

<div>
    <div class="admin-nestable-list__item__content page-admin__item__content js-admin-nestable-list-item-content" style="">
        <span>NEW PROPOSED PAGE (copy)</span>
    </div>
    <div class="admin-nestable-list__item__content page-admin__item__content js-admin-nestable-list-item-content" style="">
        <span>NEW PROPOSED PAGE (copy)</span>
    </div>
    <div class="admin-nestable-list__item__content page-admin__item__content js-admin-nestable-list-item-content" style="">
        <span>NEW PROPOSED PAGE (copy)</span>
    </div>
    <div class="admin-nestable-list__item__content page-admin__item__content js-admin-nestable-list-item-content" style="">
        <span>NEW PROPOSED PAGE (copy)</span>
    </div>
    <div class="admin-nestable-list__item__content page-admin__item__content js-admin-nestable-list-item-content" style="">
        <span>NEW PROPOSED PAGE (copy)</span>
    </div>
    <div class="admin-nestable-list__item__content page-admin__item__content js-admin-nestable-list-item-content" style="">
        <span>NEW PROPOSED PAGE (copy)</span>
    </div>
</div>

So If user clicks to the 3rd element I can get that element with xpath: //div/div[3] - But I need that in different way:

I click on the 3rd element and get this element as IWebElement object - now if I want to generate xpath - programmatically - I need to know, that I have clicked to 3rd element - so my thoughts was, that I can get the list of IWebElements:

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;
        }

Problem is that this does not work

Test #1 as @Prophet proposed: I put one of the inputs into the scriptObject.CurrentObject to test, if it will work. Before this I have trie to perform.Click() to this object and it worked - so object is accessible by the selenium.

Unfortunatetly - then I just tried to find this element/object by foreach //input - but the object was not found...

                    IList<IWebElement> elements = c.FindElements(By.XPath("//input"));
                    foreach (IWebElement e in elements)
                    {
                        if (e == scriptObject.CurrentObject)
                        {
                            string x = "OK";
                        }
                    }

This was executed when I received the Element from the click to one of the inputs in the page and then I have tried to find it again... and the element was not found.

Any advice?

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;
        }

First, in case your elements xpath is something like //tag[@att='val'] you can access n-th element by (//tag[@att='val'])[n] where n is the index from 1 to 5 in your case.
As about validating the element was clicked we need to see the web page.
Sometimes clicking elements causing some changes in that elements properties, normally the class names, in others some other elements will change but in many other cases clicking elements changes nothing.
So there is no general answer for this question and we have to see the specific page you are working with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM