Selenium Java 中 By 关键字的用途是什么

What's the purpose of By keyword in Selenium Java

示例代码:

public class RediffLoginPage

{
    Webdriver driver;
    public RediffLoginPage(Webdriver driver){
        this.driver=driver;
    }

    By username=By.xpath(".//*(@id='login1']");
    By Password=By.name("passwd");
}

public Webelement Emailid()
{
    return driver.findElement(username);
}

public Webelement Password(){return driver.findElement(Password);}

在这一行中,

By username=By.xpath(".//*(@id='login1']");

这里第一个 By 关键字的目的是什么?

它是测试用例的对象存储库代码。

Class 作者

Class By 扩展 java.lang.Object 并定义为:

public abstract class By
extends java.lang.Object

Mechanism used to locate elements within a document. In order to create your own locating mechanisms, it is possible to subclass this class and override the protected methods as required, though it is expected that all subclasses rely on the basic finding mechanisms provided through static methods of this class:

 public WebElement findElement(WebDriver driver) {
     WebElement element = driver.findElement(By.id(getSelector()));
     if (element == null)
       element = driver.findElement(By.name(getSelector());
     return element;
 }
 

By 有以下直接已知子类: