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. The programs will be separate and so I can't just send the WebElement object over a socket. How can I uniquely identify an element in a way that it can be sent over a socket and be accurate every time?
我希望能够在Selenium中唯一地标识一个元素,以便让人们A点击元素x,我可以向人B发送一个数据包,让他们知道单击了一个元素,并且该元素是x。程序将是独立的,所以我不能只通过套接字发送WebElement对象。如何以一种可以通过套接字发送并且每次都准确的方式唯一地标识元素?
I want to do this so that person A can execute actions on the browser that's actually hosted on person B's computer. 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人计算机上的浏览器上执行操作。人A对浏览器的看法与人B相同,但在浏览器上执行的所有操作实际上都是在人B的计算机上完成的,而人A的浏览器则更像是镜像(能够点击元素和类型)人B计算机上的真实浏览器。
1 个解决方案
#1
Ideally, what you should do is assign id
values to the elements you care about. 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
. It could be something as banal as numbering all button
elements from 1 in document order. The id
is what you use to uniquely identify a button
.
理想情况下,您应该做的是将id值分配给您关心的元素。该算法必须使得两个元素不能获得相同的id,但是可以预测id值的赋值,使得两个相同结构的DOM树中的相同位置中的两个元素将获得相同的id。它可能是一种平庸的事情,就像在文档顺序中对1中的所有按钮元素进行编号一样。 id是您用来唯一标识按钮的内容。
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. 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. 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
. (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. For instance, /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).
如果你不能提前知道你关心的元素,那么你必须使用另一种方法。我为我的一个应用程序做了类似的事情。我所做的是创建一个路径,其中路径的每一步都是其父节点子节点中DOM节点的索引。此路径可用于唯一标识元素。在您的情况下,作为第一个参考点的根可以是body元素。路径看起来像这样/ 1/4/1/2(向后读)指的是身体1的孩子4的孩子1的孩子2。 (索引从0开始。)如果您有一些用途,可以创建一个与XPath兼容的格式的路径。例如,/ ul [2] / li [5] / ul [2] / li [3]。 (提醒: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. 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. However, the order and number of button
elements still matter.
我使用id值描述的第一种方法更能容忍结构上的差异。如果你只关心按钮元素,并且在两侧,你有按钮元素出现在“Checkout”,“Empty Cart”,“Remove Item”的顺序中,并且它们在两侧执行相同的功能,那么它不会无论他们在各自的DOM树中拥有什么祖先。但是,按钮元素的顺序和数量仍然很重要。
#1
Ideally, what you should do is assign id
values to the elements you care about. 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
. It could be something as banal as numbering all button
elements from 1 in document order. The id
is what you use to uniquely identify a button
.
理想情况下,您应该做的是将id值分配给您关心的元素。该算法必须使得两个元素不能获得相同的id,但是可以预测id值的赋值,使得两个相同结构的DOM树中的相同位置中的两个元素将获得相同的id。它可能是一种平庸的事情,就像在文档顺序中对1中的所有按钮元素进行编号一样。 id是您用来唯一标识按钮的内容。
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. 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. 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
. (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. For instance, /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).
如果你不能提前知道你关心的元素,那么你必须使用另一种方法。我为我的一个应用程序做了类似的事情。我所做的是创建一个路径,其中路径的每一步都是其父节点子节点中DOM节点的索引。此路径可用于唯一标识元素。在您的情况下,作为第一个参考点的根可以是body元素。路径看起来像这样/ 1/4/1/2(向后读)指的是身体1的孩子4的孩子1的孩子2。 (索引从0开始。)如果您有一些用途,可以创建一个与XPath兼容的格式的路径。例如,/ ul [2] / li [5] / ul [2] / li [3]。 (提醒: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. 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. However, the order and number of button
elements still matter.
我使用id值描述的第一种方法更能容忍结构上的差异。如果你只关心按钮元素,并且在两侧,你有按钮元素出现在“Checkout”,“Empty Cart”,“Remove Item”的顺序中,并且它们在两侧执行相同的功能,那么它不会无论他们在各自的DOM树中拥有什么祖先。但是,按钮元素的顺序和数量仍然很重要。