了解Selenium中的WebElement

Selenium中到处都使用WebElement来执行各种操作。什么是WebElement?这篇文章将详细讨论WebElement。

Selenium中的WebElement是一个表示网站HTML元素的Java接口。HTML元素包含一个开始标记和一个结束标记,内容位于这两个标记之间。

HTML元素的重命名
<Tag> content </Tag>

HTML元素可以嵌套,如下所示-

<Tag1>
 <Tag2> 
   content
 </Tag2>
</Tag1>

所以,让我们回到WebElement是一个接口而不是一个类这一点。

WebElement接口的声明
public interface WebElement
extends SearchContext, TakesScreenshot

因此,WebElement扩展了另外两个接口,SearchContext和TakesScreenshot。

SearchContext接口声明了两个方法,可以根据情况帮助查找单个元素或元素列表。下面是SearchContext接口声明的方法:

  • List <WebElement>findElements(By by);
  • WebElement findElement(By by);

WebElement扩展了SearchContext接口,最后使用了上述方法。

TakesScreenshot接口只声明了一个有助于截图的方法。

  • X getScreenshotAs(OutputType target)抛出WebDriverException;

除了上述方法,WebElement接口还声明了一些方法-

  • void click()
  • public void run()
  • void sendKeys(CharSequence. keysToSend)
  • void clear()
  • String getTagName()
  • String getAttribute(String name)
  • boolean isSelected()
  • boolean isEnabled()
  • String getText()
  • List<WebElement> findElements(By by)[ WebElement还声明了SearchContext接口指定的findElements方法]
  • WebElement findElement(By by)[ WebElement还声明了findElement方法,该方法由SearchContext接口指定]
  • boolean isDisplayed()
  • getLocation()
  • getSize()
  • Rectangle getRect()
  • String getCssValue(String propertyName)

WebElement interface methods

现在的问题是,如果WebElement只是一个接口,那么哪个类实现了它的方法?

许多类实现了WebElement接口,如RemoteWebElement、HtmlUnitWebElement等,这些类实现了WebElement接口声明的方法。

我们不能在Java中创建接口的对象。但是,我们可以用接口的引用变量调用接口方法,这就是我们在调用WebElement接口的方法时所做的。

Reference variable of WebElement interface

// Here webelement is the reference variable
// of the WebElement interface
webElement.clear();
webElement.click();

但问题仍然存在。我们不能将WebElement作为一个接口来创建对象,我们需要一个实现WebElement接口声明的方法的对象。

这就是两种广泛使用的方法出现的地方。

  • List <WebElement>findElements(By by);
  • WebElement findElement(By by);

findElement方法返回一个实现WebElement接口方法的对象,而findElements返回这些对象的列表。

因此,每当我们编写WebElement element = findElement(By.id(“firstName”))时,它实际上使用WebElement接口引用变量并调用由findElement方法返回的对象的实现。

WebElement可以是任何类型。它可以是文本、按钮、复选框或HTML元素。WebElement方法不能应用于每个元素类型,就像我们不能清除按钮一样,所以我们不应该对按钮元素类型使用clear()方法。使用元素类型不支持的方法可能会也可能不会导致错误。

Selenium中的WebElement方法列表
方法名称这个方法做什么?
void clear()如果元素是文本输入元素,则此操作将清除该值,并且对其他元素没有影响。
void click()它点击元素。
WebElement findElement(By by)它查找与定位条件匹配的第一个元素。
List<WebElement> findElements(By by)它查找所有与定位条件匹配的元素。
String getAttribute(String name)它获取元素的给定属性的值
String getCssValue(String propertyName)它获取给定CSS属性的值。
getLocation()它返回一个包含元素左上角位置的点。
Rectangle getRect()它返回所呈现元素的位置和大小。
<X> X getScreenshotAs(OutputType<X> target)
 
它捕获屏幕截图并将其存储在指定位置。
getSize()它返回页面上元素的大小。
String getTagName()它返回此元素的标记名称。
String getText()它返回元素的可见文本,包括子元素。
boolean isDisplayed()它告诉元素是否显示。
boolean isEnabled()它说明元素当前是否已启用。
boolean isSelected()它告诉元素是否被选中。
void sendKeys(CharSequence. keysToSend)它模拟输入元素。
void submit()如果当前元素是一个表单或表单中的一个元素,那么它将被提交到远程服务器。
  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知识的宝藏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值