官术网_书友最值得收藏!

Dissecting the By class

The By class is an abstract class that has eight static methods and eight inner classes. Let's understand the structure of the By class.

The following code skeleton shows a fragment of the structure of the By class:

public abstract class By {
public static By id(String id);
public static By cssSelector(String css);
public static By name(String name);
public static By linkText(String text);
public static By className(String className);
public static By tagName(String tagName);
public static By partialLinkText(String partialLinkText);
public static By xpath(String xpath);
public static class ById extends By {
WebElement findElement(By by);
List <WebElement> findElements(By by);
}
}
Note: Inner classes are present corresponding to all the static methods. There are inner classes such as ByName, ByTagName, and so on.

Inner classes similar to ById exist for name, linkText, xpath, and so on. We will be using the two static methods xpath and cssSelector to design what are called customized xpath and css. Let's try to understand the various mechanisms to access the DOM elements. There are eight ways to access WebElements using the static methods in the By class. We will just be covering access using static methods.

We will look at each of the eight methods individually, and then we will adopt a better approach using the relative (customized) XPath, which will cover each method internally:

  • By.id: Uses the id attribute of the element to locate. For example, By.id("userid").
  • By.name: Uses the name attribute of the element to locate. For example, By.name("username").
  • By.className: Uses the class attribute of the element to locate. For example, By.className("class1").
  • By.linkTextUses the text of any anchor link to locate. For example, By.linkText("Click here to Login").
  • By.partialLinkTextUses the partial text of any anchor link to locate. For example, By.partialLinkText("Login"). 
  • By.xpath: Uses the XPath of the element to locate. For example, By.xpath("http://*[text()='Login']").
  • By.cssSelector: Uses CSS selectors to locate. For example, By.cssSelector(".ctrl-p"). 
  • By.tagName: Uses tag names, such as input, button, select, and so on to locate. For example, By.tagName("input").
主站蜘蛛池模板: 清丰县| 浦县| 延长县| 冕宁县| 玛纳斯县| 绍兴县| 科尔| 太谷县| 鸡东县| 五莲县| 武陟县| 慈利县| 荣昌县| 肥城市| 永年县| 屏边| 竹北市| 嵩明县| 杭锦后旗| 通州区| 丽江市| 沂源县| 鹤岗市| 大宁县| 龙胜| 和龙市| 凤山县| 西青区| 横峰县| 陕西省| 屏边| 沭阳县| 沾化县| 富川| 通渭县| 丘北县| 赤水市| 平安县| 棋牌| 宁津县| 高安市|