为什么findElement(By by)的返回类型是WebElement? [英] Why return type of findElement(By by) is WebElement?

查看:509
本文介绍了为什么findElement(By by)的返回类型是WebElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么findElement(by by)的返回类型是WebElement?

我知道文档中已经提到返回类型为Webelment,但是任何人都可以解释一下为什么返回类型为Webelment.

I know it has mention in documentation that return type is Webelment,But can anyone please explain me why return type is Webelment.

我对此主题的研究.

因为我们正在使用findElement搜索元素,而webelement类用于表示HTML元素.

Because we are searching elements using findElement and webelement class is used to represent HTML element.

如果我想的正确或有任何特定原因,请告诉我

Please let me know If I am thinking right or there any specific reason

推荐答案

您看对了.根据 Java文档

You saw it right. As per the Java Docs findElement() method returns a Webelment.

要回答您的问题为什么返回类型是Webelment ,与您相反的问题是为什么要找到该元素?.

To answer your question why return type is Webelment a counter question to you would be Why are you finding the element?.

粗略的答案是,您需要与元素进行交互(以某种方式):

Of-coarse the answer is you need to interact (in some way) with the element which may be either of the following :

  • 标题
  • 文本字段
  • 密码字段
  • 按钮
  • 选择元素

您正在搜索 WebElement :

  • 声明WebElement条件(isDisplayed()isEnabled()).
  • 与其互动(click()sendKeys()).
  • 检索其任何属性. (getAttribute()getCssValue())
  • 等待 WebElement 处于特定条件(isSelected()).
  • Assert the WebElement condition (isDisplayed(), isEnabled()).
  • Interact with it (click(), sendKeys()).
  • Retrive any of its attributes. (getAttribute(), getCssValue())
  • Wait for the WebElement to be in certain condition (isSelected()).

因此,出于上述两个原因之一,您都需要获取 WebElement 的引用,并通过 findElement(by by)进行传递;如果搜索成功,则确切的 WebElement 返回.现在,您可以调用任何已注册的 WebElement 上,具体如下:

So for either of those reasons you need to get the reference of the WebElement which you pass through findElement(By by) and if your search is successful the exact WebElement is returned back. Now you can invoke any of the enlisted methods() on the WebElement as per feasibility as follows :

  • clear()
  • click()
  • findElement(By by)
  • findElements(By by)
  • getAttribute(java.lang.String name)
  • getCssValue(java.lang.String propertyName)
  • getLocation()
  • getSize()
  • getTagName()
  • getText()
  • isDisplayed()
  • isEnabled()
  • isSelected()
  • sendKeys(java.lang.CharSequence... keysToSend)
  • submit()
  • clear()
  • click()
  • findElement(By by)
  • findElements(By by)
  • getAttribute(java.lang.String name)
  • getCssValue(java.lang.String propertyName)
  • getLocation()
  • getSize()
  • getTagName()
  • getText()
  • isDisplayed()
  • isEnabled()
  • isSelected()
  • sendKeys(java.lang.CharSequence... keysToSend)
  • submit()

如果 findElement()的返回类型为 Boolean (根据您的评论更新),则不可能调用以下任何methods().

Invoking any of the following methods() wouldn't have been possible if the return type of findElement() had been Boolean (as per your comment update).

这篇关于为什么findElement(By by)的返回类型是WebElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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