Class By

java.lang.Object
org.jboss.elemento.By

public abstract class By extends Object
Typesafe CSS selector API.

Use the static methods in this class to create arbitrary complex CSS selectors:

// #main [data-list-item=foo] a[href^="https://"] > .fas.fa-check, .external[hidden]
By.group(
        By.id("main")
                .desc(By.data("listItem", "foo")
                        .desc(By.element("a").and(By.attribute("href", STARTS_WITH, "https://"))
                                .child(By.classnames("fas", "fa-check")))),
        By.classname("external").and(By.attribute("hidden")));
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Operator used for attribute selectors.
  • Constructor Summary

    Constructors
    Constructor
    Description
    By()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final By
    adjacentSibling(By selector)
    Combines this selector with the given selector using the + (adjacent sibling combinator) combinator.
    final By
    and(By selector)
    Combines this selector with the given selector.
    static By
    Selects all elements that have an attribute name of name.
    static By
    attribute(String name, String value)
    Selects all elements that have an attribute name of name whose value is exactly value.
    static By
    attribute(String name, By.AttributeOperator operator, String value)
    Selects all elements that have an attribute name of name whose value applies to the given operator.
    final By
    child(By selector)
    Combines this selector with the given selector using the > (child) combinator.
    static By
    classname(String classname)
    Selects all elements that have the given class attribute.
    static By
    classnames(String[] classnames)
    Selects all elements that have all class attributes.
    static By
    classnames(String first, String second, String... remaining)
    Selects all elements that have all class attributes.
    static By
    data(String name)
    Selects all elements that have an attribute name of data-name.
    static By
    data(String name, String value)
    Selects all elements that have an attribute name of data-name whose value is exactly value.
    static By
    data(String name, By.AttributeOperator operator, String value)
    Selects all elements that have an attribute name of data-name whose value applies to the given operator.
    final By
    desc(By selector)
    Combines this selector with the given selector using the (space) combinator.
    static By
    element(Element element)
    Selects all elements that have the given node name.
    static By
    element(String element)
    Selects all elements that have the given node name.
    static <E extends HTMLElement>
    By
    element(IsElement<E> element)
    Selects all elements that have the given node name.
    boolean
     
    static By
    group(By[] selectors)
    Groups the specified selectors using ,.
    static By
    group(By first, By second, By... remaining)
    Groups the specified selectors using ,.
    int
     
    static By
    id(String id)
    Selects an element based on the value of its id attribute.
    static By
    selector(String selector)
    Returns a selector as-is.
    final By
    sibling(By selector)
    Combines this selector with the given selector using the ~ (general sibling) combinator.
     

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • By

      public By()
  • Method Details

    • selector

      public static By selector(String selector)
      Returns a selector as-is.
    • id

      public static By id(String id)
      Selects an element based on the value of its id attribute.
    • element

      public static By element(String element)
      Selects all elements that have the given node name.
    • element

      public static By element(Element element)
      Selects all elements that have the given node name.
    • element

      public static <E extends HTMLElement> By element(IsElement<E> element)
      Selects all elements that have the given node name.
    • classname

      public static By classname(String classname)
      Selects all elements that have the given class attribute.
    • classnames

      public static By classnames(String first, String second, String... remaining)
      Selects all elements that have all class attributes.
    • classnames

      public static By classnames(String[] classnames)
      Selects all elements that have all class attributes.
    • attribute

      public static By attribute(String name)
      Selects all elements that have an attribute name of name.
    • attribute

      public static By attribute(String name, String value)
      Selects all elements that have an attribute name of name whose value is exactly value.

      You don't need to enclose the value in single or double quotes. If necessary, quotes are added automatically.

      See Also:
    • attribute

      public static By attribute(String name, By.AttributeOperator operator, String value)
      Selects all elements that have an attribute name of name whose value applies to the given operator.

      You don't need to enclose the value in single or double quotes. If necessary, quotes are added automatically.

      See Also:
    • data

      public static By data(String name)
      Selects all elements that have an attribute name of data-name.

      If name contains "-" it is used as if, otherwise it is expected to be in camelCase and is converted to kebab-case.

    • data

      public static By data(String name, String value)
      Selects all elements that have an attribute name of data-name whose value is exactly value.

      If name contains "-" it is used as if, otherwise it is expected to be in camelCase and is converted to kebab-case.

      You don't need to enclose the value in single or double quotes. If necessary, quotes are added automatically.

      See Also:
    • data

      public static By data(String name, By.AttributeOperator operator, String value)
      Selects all elements that have an attribute name of data-name whose value applies to the given operator.

      If name contains "-" it is used as if, otherwise it is expected to be in camelCase and is converted to kebab-case.

      You don't need to enclose the value in single or double quotes. If necessary, quotes are added automatically.

      See Also:
    • group

      public static By group(By first, By second, By... remaining)
      Groups the specified selectors using ,.

      // #main [data-list-item=foo] a[href^="https://"] > .fas.fa-check, .external[hidden]
      By.group(
              By.id("main")
                      .desc(By.data("listItem", "foo")
                              .desc(By.element("a").and(By.attribute("href", STARTS_WITH, "https://"))
                                      .child(By.classnames("fas", "fa-check")))),
              By.classname("external").and(By.attribute("hidden")));
      
    • group

      public static By group(By[] selectors)
      Groups the specified selectors using ,.

      // #main [data-list-item=foo] a[href^="https://"] > .fas.fa-check, .external[hidden]
      By.group(
              By.id("main")
                      .desc(By.data("listItem", "foo")
                              .desc(By.element("a").and(By.attribute("href", STARTS_WITH, "https://"))
                                      .child(By.classnames("fas", "fa-check")))),
              By.classname("external").and(By.attribute("hidden")));
      
    • and

      public final By and(By selector)
      Combines this selector with the given selector. Use this method to express selectors like button.primary or input[type=checkbox]:

      // button.primary
      By.element("button").and(By.classname("primary"));
      
      // input[type=checkbox]
      By.element("input").and(By.attribute("type", "checkbox"));
      
    • child

      public final By child(By selector)
      Combines this selector with the given selector using the > (child) combinator. Selects nodes that are direct children of this element.
    • desc

      public final By desc(By selector)
      Combines this selector with the given selector using the (space) combinator. Selects nodes that are descendants of this element.
    • sibling

      public final By sibling(By selector)
      Combines this selector with the given selector using the ~ (general sibling) combinator. This means that selector follows this element (though not necessarily immediately), and both share the same parent.
    • adjacentSibling

      public final By adjacentSibling(By selector)
      Combines this selector with the given selector using the + (adjacent sibling combinator) combinator. This means that selector directly follows this element, and both share the same parent.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object