简体   繁体   English

如何唯一标识硒中的元素?

[英]How to uniquely identify an element in Selenium?

I want to be able to uniquely identify an element in Selenium so that, let's say person A clicks on element x, I can send a packet to person B letting them know that an element was clicked and that the element is x. 我希望能够唯一地标识Selenium中的元素,以便假设某人A单击元素x,我可以向某人B发送一个数据包,让他们知道该元素已被单击并且该元素是x。 The programs will be separate and so I can't just send the WebElement object over a socket. 程序是分开的,所以我不能只通过套接字发送WebElement对象。 How can I uniquely identify an element in a way that it can be sent over a socket and be accurate every time? 如何以一种可以通过套接字发送且每次都准确的方式唯一地标识一个元素?

I want to do this so that person A can execute actions on the browser that's actually hosted on person B's computer. 我想这样做,以便A人可以在B人计算机上实际托管的浏览器上执行操作。 Person A's view of the browser would be identical to person B's, but all actions executed on the browser would actually be done on person B's computer, and person A's browser would be more of a mirror (with the ability to click elements and type) of the real browser on person B's computer. A人对浏览器的看法与B人的看法相同,但实际上在B人的浏览器上执行的所有操作都将在B人的计算机上完成,A人的浏览器更像是一个镜子(具有单击元素和类型的能力)。乙人计算机上的真实浏览器。

Ideally, what you should do is assign id values to the elements you care about. 理想情况下,您应该为您关心的元素分配id值。 The algorithm would have to be such that two elements cannot get the same id but that the assignment of id values can be predicted so that two elements in the same location in two identically structured DOM trees will get the same id . 该算法必须使得两个元素不能获得相同的id但可以预测id值的分配,以便两个结构相同的DOM树中相同位置的两个元素将获得相同的id It could be something as banal as numbering all button elements from 1 in document order. 可能像在文档顺序中从1开始对所有button元素编号一样平庸。 The id is what you use to uniquely identify a button . id是用来唯一标识buttonid

If you cannot know the elements you care about ahead of time, then you have to use another method. 如果您无法提前知道自己关心的元素,则必须使用另一种方法。 I've done something like this for one of my applications. 我已经为我的一个应用程序做了类似的事情。 What I do is create a path where each step of the path is the index of the DOM node among the children of its parent. 我要做的是创建一条路径,其中路径的每一步都是DOM节点在其父级子级中的索引。 This path can be used to uniquely identify an element. 此路径可用于唯一标识元素。 In your case, the root that serves as the first reference point could be the body element. 在您的情况下,用作第一个参考点的根可能是body元素。 A path would look something like this /1/4/1/2 which (reading backwards) refers to child 2 of the child 1 of the child 4 of the child 1 of body . 路径看起来像这样的/1/4/1/2 ,它(向后读)指的是body的child 1的child 4的child 1的child 2。 (The indexes are 0-based.) It would be possible to create an path in a format that would be compatible with XPath if you have some use for that. (索引是从0开始的。)如果有某种用途,则可以使用与XPath兼容的格式来创建路径。 For instance, /ul[2]/li[5]/ul[2]/li[3] . 例如, /ul[2]/li[5]/ul[2]/li[3] (Reminder: XPath indexes are 1-based.) In my project, I don't need XPath compatibility (nor is it beneficial in any way in my project ). (提醒:XPath索引是基于1的。)在我的项目中,我不需要XPath兼容性( 在我的项目中也不会有任何好处)。

There is one major caveat: The DOM tree on both sides have to have the same structure. 有一个主要警告: 两侧的DOM树必须具有相同的结构。

For instance. 例如。 If on one side, you have: 如果在一侧,则具有:

<body>
  <ul>
    <li>foo</li>
    <li>bar</li>
  </ul>
<body>

Then you should have the same on the other side, because if someone clicks on the last li and you need to identify the element on the other side, you'll have a problem if the DOM tree is representing this: 然后,您应该在另一侧具有相同的功能,因为如果有人单击最后一个li并且您需要在另一侧标识该元素,那么如果DOM树表示此元素,则会遇到问题:

<body>
  <p>foo</p>
<body>

How you ensure that your trees have the same structure is wholly dependent on your specific application. 如何确保树具有相同的结构完全取决于您的特定应用程序。

The first method I described using id values is more tolerant of differences in structure. 我使用id值描述的第一种方法更能容忍结构差异。 If you care only about button elements and, on both sides, you have button elements that appear in the order "Checkout", "Empty Cart", "Remove Item" and that they perform the same functions on both sides, then it does not matter what ancestors they have in their respective DOM tree. 如果您只关心button元素,并且在两侧都有button “签出”,“空购物车”,“删除商品”的顺序显示的button元素,并且它们在两侧都执行相同的功能,那么它就不会无论他们在各自的DOM树中拥有哪些祖先。 However, the order and number of button elements still matter. 但是, button元素的顺序和数量仍然很重要。

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

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