Package org.openqa.selenium
Class By
- java.lang.Object
-
- 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 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; }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description 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
static interface
By.Remotable
-
Constructor Summary
Constructors Constructor Description By()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static By
className(java.lang.String className)
Find elements based on the value of the "class" attribute.static By
cssSelector(java.lang.String cssSelector)
Find elements via the driver's underlying W3C Selector engine.boolean
equals(java.lang.Object o)
WebElement
findElement(SearchContext context)
Find a single element.abstract java.util.List<WebElement>
findElements(SearchContext context)
Find many elements.protected JavascriptExecutor
getJavascriptExecutor(SearchContext context)
protected WebDriver
getWebDriver(SearchContext context)
int
hashCode()
static By
id(java.lang.String id)
static By
linkText(java.lang.String linkText)
static By
name(java.lang.String name)
static By
partialLinkText(java.lang.String partialLinkText)
static By
tagName(java.lang.String tagName)
java.lang.String
toString()
static By
xpath(java.lang.String xpathExpression)
-
-
-
Method Detail
-
id
public static By id(java.lang.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(java.lang.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(java.lang.String partialLinkText)
- Parameters:
partialLinkText
- The partial text to match against- Returns:
- a By which locates elements that contain the given link text.
-
name
public static By name(java.lang.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(java.lang.String tagName)
- Parameters:
tagName
- The element's tag name.- Returns:
- A By which locates elements by their tag name.
-
xpath
public static By xpath(java.lang.String xpathExpression)
- Parameters:
xpathExpression
- The XPath to use.- Returns:
- A By which locates elements via XPath.
-
className
public static By className(java.lang.String className)
Find elements based on the value of the "class" attribute. Only one class name should be used. If an element has multiple classes, please usecssSelector(String)
.- 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(java.lang.String cssSelector)
Find elements via the driver's underlying W3C 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.- Parameters:
cssSelector
- CSS expression.- Returns:
- A By which locates elements by CSS.
-
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 java.util.List<WebElement> findElements(SearchContext context)
Find many elements.- Parameters:
context
- A context to use to find the elements.- Returns:
- A list of WebElements matching the selector.
-
getWebDriver
protected WebDriver getWebDriver(SearchContext context)
-
getJavascriptExecutor
protected JavascriptExecutor getJavascriptExecutor(SearchContext context)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-