【问题标题】:What is the purpose of using 'By' in the method arguments?在方法参数中使用“By”的目的是什么?
【发布时间】:2020-02-07 05:43:40
【问题描述】:

在下面的方法中,By 用作参数。谁能解释一下它的用途。

public boolean click(By by, String...elementName) {
    try {
        getElement(by).click();
        if(elementName.length != 0)
            report("PASS", elementName[0] + " is clicked successfully", false);
        return true;
    }
}

【问题讨论】:

标签: java selenium


【解决方案1】:

By 是 Selenium 中的一个类。它是一种用于在文档中定位元素的机制。各种选项包括

  • 按类名
  • 通过CssSelector
  • 按 ID
  • ByLinkText
  • 按姓名
  • ByPartialLinkText
  • 按标签名
  • 通过XPath

您将传递给您的方法的任何参数(来自上述之一)都将用于识别对象。

它(可能已经)用作参数的原因是为了使方法尽可能动态。

正如@Dinar Zaripov 的评论中所显示的,this 总是很适合访问。

【讨论】:

  • 解释得很好。
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多