Implementation:SeleniumHQ Selenium WebDriver Navigation And Element Interaction
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, WebDriver, DOM_Interaction |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete tool for navigating web pages and interacting with DOM elements provided by the Selenium WebDriver Java interface.
Description
The WebDriver interface (and its Navigation sub-interface) provides page navigation methods. The By abstract class provides element location strategies (id, CSS selector, XPath, link text, partial link text, name, tag name, class name). The WebElement interface provides methods for interacting with found elements (click, sendKeys, getText, getAttribute, getDomProperty, getDomAttribute, isDisplayed, isEnabled, isSelected). Together, these three classes form the core interaction API of Selenium WebDriver. The By class uses an internal BaseW3CLocator for W3C-native strategies and a PreW3CLocator for legacy strategies (id, name, className) that fall back to CSS selectors.
Usage
These APIs are used after session creation for all browser automation tasks: loading pages, finding elements, filling forms, clicking buttons, and reading page content.
Code Reference
Source Location
- Repository: Selenium
- File: java/src/org/openqa/selenium/WebDriver.java (L49-610)
- File: java/src/org/openqa/selenium/WebElement.java (L32-335)
- File: java/src/org/openqa/selenium/By.java (L44-457)
Signature
// WebDriver interface (extends SearchContext)
public interface WebDriver extends SearchContext {
void get(String url);
@Nullable String getCurrentUrl();
@Nullable String getTitle();
@Nullable String getPageSource();
void close();
void quit();
Set<String> getWindowHandles();
String getWindowHandle();
TargetLocator switchTo();
Navigation navigate();
Options manage();
WebElement findElement(By by);
List<WebElement> findElements(By by);
}
// Navigation sub-interface
interface Navigation {
void back();
void forward();
void to(String url);
void to(URL url);
void refresh();
}
// WebElement interface (extends SearchContext, TakesScreenshot)
public interface WebElement extends SearchContext, TakesScreenshot {
void click();
void submit();
void sendKeys(CharSequence... keysToSend);
void clear();
String getTagName();
@Nullable String getDomProperty(String name);
@Nullable String getDomAttribute(String name);
@Nullable String getAttribute(String name);
@Nullable String getAriaRole();
@Nullable String getAccessibleName();
boolean isSelected();
boolean isEnabled();
String getText();
boolean isDisplayed();
Point getLocation();
Dimension getSize();
Rectangle getRect();
String getCssValue(String propertyName);
SearchContext getShadowRoot();
WebElement findElement(By by);
List<WebElement> findElements(By by);
}
// By class (element locator strategies)
public abstract class By {
public static By id(String id);
public static By linkText(String linkText);
public static By partialLinkText(String partialLinkText);
public static By name(String name);
public static By tagName(String tagName);
public static By xpath(String xpathExpression);
public static By className(String className);
public static By cssSelector(String cssSelector);
}
Import
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | String | Yes (for get()) | Fully qualified URL to navigate to |
| by | By | Yes (for findElement()) | Element locator strategy |
| keysToSend | CharSequence... | Yes (for sendKeys()) | Text to type or Keys enum values |
Outputs
| Name | Type | Description |
|---|---|---|
| findElement() | WebElement | First matching DOM element (throws NoSuchElementException if none found) |
| findElements() | List<WebElement> | All matching DOM elements (empty list if none found) |
| getText() | String | Visible text content of an element |
| getAttribute() | String | Value of the specified element attribute/property (null if not set) |
| getDomProperty() | String | Value of the specified element DOM property (null if not set) |
| getDomAttribute() | String | Value of the specified element HTML attribute (null if not set) |
| getCurrentUrl() | String | Current page URL |
| getTitle() | String | Current page title |
Usage Examples
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
WebDriver driver = new ChromeDriver();
// Navigate to a page
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
// Find elements using different strategies
WebElement textInput = driver.findElement(By.id("my-text-id"));
WebElement submitBtn = driver.findElement(By.cssSelector("button[type='submit']"));
// Interact with elements
textInput.sendKeys("Hello Selenium");
submitBtn.click();
// Read page state
String title = driver.getTitle();
String url = driver.getCurrentUrl();
driver.quit();
Multiple Element Strategies
// By ID (falls back to CSS selector: #username)
WebElement byId = driver.findElement(By.id("username"));
// By CSS Selector (native W3C strategy)
WebElement byCss = driver.findElement(By.cssSelector(".login-form input[name='user']"));
// By XPath (native W3C strategy)
WebElement byXpath = driver.findElement(By.xpath("//input[@placeholder='Username']"));
// By Name (falls back to CSS selector: *[name='password'])
WebElement byName = driver.findElement(By.name("password"));
// By Tag Name (native W3C strategy)
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
// By Link Text (native W3C strategy)
WebElement byLink = driver.findElement(By.linkText("Click Here"));
// By Partial Link Text (native W3C strategy)
WebElement byPartial = driver.findElement(By.partialLinkText("Click"));
// By Class Name (falls back to CSS selector: .my-class)
WebElement byClass = driver.findElement(By.className("my-class"));
import org.openqa.selenium.WebDriver;
WebDriver.Navigation nav = driver.navigate();
nav.to("https://example.com");
nav.back();
nav.forward();
nav.refresh();