WebElement.equals() 方法如何检查相等性? [英] How Does WebElement.equals() Method Check for Equality?

查看:43
本文介绍了WebElement.equals() 方法如何检查相等性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有超过 1 个 XPath 指向一个 Web 元素,我想确定这两个元素是否等效(即,如果我在两个 Web 元素上执行操作 sendKeys() 或 click(),该操作将在相同的网页元素)目前我正在使用以下方法检查相等性:

I have more than 1 XPath pointing to a web element and I want to determine if both elements are equivalent (i.e, if I perform the action sendKeys() or click() on both web elements the action would be executed on the same web element) Currently I'm checking for equality using the following method:

    WebElement element1 = driver.findElement(By.xpath(".//*[@id='ap_email']"));
    WebElement element2 = driver.findElement(By.xpath(".//*[@type='email']"));


    System.out.println(element1.equals(element2));

只要两个 XPath 都指向同一个元素,这将返回 true.

This will return true as long as both XPath point to the same element.

但是,我不确定该方法如何确定相等性?是否存在两个 Web 元素实际上相同的情况(即,对它们执行的任何操作都将在同一个 Web 元素上执行)但方法显示它们不同?反之亦然?

However, I am unsure how the method determines equality? And are there any circumstances where the two web elements are in fact the same (i.e, any action carried out on them would be carried out on the same web element) but the method shows they are different? Or vice versa?

我正在尝试开发一种万无一失的方法来确定两个 Web 元素的相等性.

I am trying to develop a foolproof method to determine equality of two web elements.

我正在使用 Java 和 Selenium.

I'm using Java and Selenium.

推荐答案

但是,我不确定该方法如何确定相等性?

However, I am unsure how the method determines equality?

驱动程序为文档中遇到的每个 HTMLElement 对象生成一个引用,并始终为 HTMLElement 对象返回相同的引用.

The driver generates a reference for each encountered HTMLElement object in the document and always returns the same reference for an HTMLElement object.

此引用存储在客户端的 WebElement.id 属性中,与用于定位器的 id 属性无关.

This reference is stored in the WebElement.id property on the client side and has nothing to do with the id attribute used with a locator.

因此,为了确定两个 WebElement 之间的相等性,客户端只需测试存储在 WebElement.id 中的引用.

So to determine the equality between two WebElement, the client simply tests the reference stored in WebElement.id.

是否存在两种网络元素实际上相同但方法显示它们不同的情况?

And are there any circumstances where the two web elements are in fact the same but the method shows they are different?

从技术上讲,不,它永远不会发生,如果是这样,那将是一个严重的问题.

Technically speaking, no it never happens and it would be a serious issue if it was the case.

但这有点取决于你的意思.例如,一个按钮在两个不同的时刻可能看起来相同,但可能已被替换为相同的按钮,从而生成新的引用.这是页面或某些部分重新加载时的情况:

But it kind of depends on what you mean by the same. For instance a button can look the same at two different moments, but may have been replaced by an identical one which would generate a new reference. This is the case when the page or some parts are reloaded:

  driver.get("http://stackoverflow.com/");

  WebElement elementA = driver.findElement(By.cssSelector("#logo"));
  WebElement elementB = driver.findElement(By.cssSelector("#logo"));

  boolean same1 = elementA.equals(elementB);  // return true

  elementB.click();  // reloads the page, all the previous web element are now obsolete

  WebElement elementC = driver.findElement(By.cssSelector("#logo"));

  boolean same2 = elementA.Equals(elementC);  // return false

这篇关于WebElement.equals() 方法如何检查相等性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆