简体   繁体   English

SELENIUM - 如何计算 IList 中的 IWebElement<iwebelement> 并在列表中获取 position</iwebelement>

[英]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).我在页面上有 5 个完全相同的元素(相同的 div,相同的 class,没有 id,相同的内部文本)。 So I can build the xpath - where Selenium returns to me - the IList with IWebElements.所以我可以构建 xpath - 其中 Selenium 返回给我 - 带有 IWebElements 的 IList。 My Goal is to iterate thru this list and "somehow" get the IWebElement which I have clicked on....我的目标是遍历此列表并“以某种方式”获取我单击的 IWebElement ....

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?我知道,我可以像这样使用 xpath : //div[2] 用于第二个,但这就是问题 - 我如何从列表中获取单击第二个 WebElement 的信息? Can I somehow compare the elements?我可以以某种方式比较元素吗? (I have tried compare GetHas() and compare the webelements - but both did not worked) (我试过比较 GetHas() 并比较 webelements - 但两者都没有工作)

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我的代码获取用户单击列表中第 n 个元素的信息
  • My code will generate xpath -> //div[x] where x is the order of the element in the list我的代码将生成 xpath -> //div[x] 其中 x 是列表中元素的顺序

Lets see the real example - I ve got this HTML:让我们看看真实的例子 - 我有这个 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:因此,如果用户单击第三个元素,我可以使用 xpath: //div/div[3] 获取该元素 - 但我需要以不同的方式:

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:我单击第三个元素并将该元素作为 IWebElement object - 现在如果我想生成 xpath - 以编程方式 - 我需要知道,我已经点击了第三个元素 - 所以我的想法是,我可以获得 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. @Prophet 建议的测试#1:我将其中一个输入放入 scriptObject.CurrentObject 进行测试,如果它可以工作。 Before this I have trie to perform.Click() to this object and it worked - so object is accessible by the selenium.在此之前,我尝试执行此 object 并单击()并且它有效 - 因此 selenium 可以访问 object。

Unfortunatetly - then I just tried to find this element/object by foreach //input - but the object was not found...不幸的是 - 然后我只是试图通过 foreach //input 找到这个元素/对象 - 但没有找到 object...

                    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:我实际上可以使用上面的代码,但我需要检查 HasCode - 而不是 object 本身:

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.首先,如果您的元素xpath类似于//tag[@att='val']您可以通过(//tag[@att='val'])[n]访问第 n 个元素,其中n是索引在你的情况下从 1 到 5。
As about validating the element was clicked we need to see the web page.关于验证元素是否被点击,我们需要查看 web 页面。
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.有时单击元素会导致该元素属性发生一些变化,通常是 class 名称,在其他情况下,其他一些元素会发生变化,但在许多其他情况下,单击元素不会改变任何内容。
So there is no general answer for this question and we have to see the specific page you are working with.所以这个问题没有通用答案,我们必须查看您正在使用的特定页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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