org.openqa.selenium
Class By

java.lang.Object
  extended by org.openqa.selenium.By
Direct Known Subclasses:
By.ByClassName, By.ByCssSelector, By.ById, By.ByLinkText, By.ByName, By.ByPartialLinkText, By.ByTagName, By.ByXPath

public abstract class By
extends 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 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; }


Nested Class Summary
static class By.ByClassName
           
static class By.ByCssSelector
           
static class By.ById
           
static class By.ByLinkText
           
static class By.ByName
           
static class By.ByPartialLinkText
           
static class By.ByTagName
           
static class By.ByXPath
           
 
Constructor Summary
By()
           
 
Method Summary
static By className(String className)
          Finds elements based on the value of the "class" attribute.
static By cssSelector(String selector)
          Finds elements via the driver's underlying W3 Selector engine.
 boolean equals(Object o)
           
 WebElement findElement(SearchContext context)
          Find a single element.
abstract  List<WebElement> findElements(SearchContext context)
          Find many elements.
 int hashCode()
           
static By id(String id)
           
static By linkText(String linkText)
           
static By name(String name)
           
static By partialLinkText(String linkText)
           
static By tagName(String name)
           
 String toString()
           
static By xpath(String xpathExpression)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

By

public By()
Method Detail

id

public static By id(String id)
Parameters:
id - The value of the "id" attribute to search for
Returns:
a By which locates elements by the value of the "id" attribute.

linkText

public static By linkText(String linkText)
Parameters:
linkText - The exact text to match against
Returns:
a By which locates A elements by the exact text it displays

partialLinkText

public static By partialLinkText(String linkText)
Parameters:
linkText - The text to match against
Returns:
a By which locates A elements that contain the given link text

name

public static By name(String name)
Parameters:
name - The value of the "name" attribute to search for
Returns:
a By which locates elements by the value of the "name" attribute.

tagName

public static By tagName(String name)
Parameters:
name - The element's tagName
Returns:
a By which locates elements by their tag name

xpath

public static By xpath(String xpathExpression)
Parameters:
xpathExpression - The xpath to use
Returns:
a By which locates elements via XPath

className

public static By className(String className)
Finds elements based on the value of the "class" attribute. If an element has many classes then this will match against each of them. For example if the value is "one two onone", then the following "className"s will match: "one" and "two"

Parameters:
className - The value of the "class" attribute to search for
Returns:
a By which locates elements by the value of the "class" attribute.

cssSelector

public static By cssSelector(String selector)
Finds elements via the driver's underlying W3 Selector engine. If the browser does not implement the Selector API, a best effort is made to emulate the API. In this case, we strive for at least CSS2 support, but offer no guarantees.


findElement

public WebElement findElement(SearchContext context)
Find a single element. Override this method if necessary.

Parameters:
context - A context to use to find the element
Returns:
The WebElement that matches the selector

findElements

public abstract List<WebElement> findElements(SearchContext context)
Find many elements.

Parameters:
context - A context to use to find the element
Returns:
A list of WebElements matching the selector

equals

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

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2012. All Rights Reserved.