SearchContext and WebDriver Methods
SearchContext is the topmost interface in the Selenium API, with two methods:
-
findElement() -
findElements()Advertisement
The WebDriver interface has many abstract methods, including:
-
get(String url) -
quit() -
close() -
getWindowHandle() -
getWindowHandles() -
getTitle()
WebDriver also has nested interfaces like Window, Navigation, and Timeouts, which are used to perform operations like back() and forward().
Key WebDriver Commands
// Navigating to URLs
driver.get("https://www.example.com");
driver.navigate().to("https://www.example.com");
// Finding web elements
WebElement element = driver.findElement(By.id("username"));
List<WebElement> elements = driver.findElements(By.className("button"));
// Interacting with web elements
element.click();
element.sendKeys("text");
WebElement Methods
findElement()
Finds the first matching element within the current page.
Return Type: WebElement
If no element is found, it throws NoSuchElementException.
WebElement element = driver.findElement(By.locator("value"));
sendKeys()
Enters a value into an edit box or text box.
driver.findElement(By.elementLocator("value")).sendKeys("value");
clear()
Clears the value from an edit box or text box.
driver.findElement(By.locatorName("value")).clear();
click()
Clicks any element such as a button, link, or checkbox.
driver.findElement(By.elementLocator("locatorValue")).click();
submit()
Clicks an element with two conditions:
-
The button must be present within a
<form>tag. -
The button type must be submit.
Element State Methods
-
isEnabled() -
isSelected() -
isDisplayed()
These methods are explained later in this document.
get() vs navigate()
Both get(String url) and navigate().to(String url) are used to open a web page.
get()
-
Directly loads the webpage.
-
Waits until the page is fully loaded.
navigate().to()
-
Part of the Navigation interface.
-
Supports:
-
Back
-
Forward
-
Refresh
-
-
More flexible for browser navigation.
Analogy
get() is like typing a URL and pressing Enter.
navigate().to() is like using a remote control that also lets you go back, forward, or refresh.
Real-time Example (E-commerce Site)
driver.get("https://amazon.in"); // Open the home page
driver.navigate().to("https://flipkart.com"); // Go to another site
driver.navigate().back(); // Come back
driver.navigate().forward();
driver.navigate().refresh();
findElement() vs findElements()
| Feature | findElement() | findElements() |
|---|---|---|
| Accesses | A single web element | All matching elements |
| Returns | First matching element (WebElement) |
List of matching elements |
| If Not Found | Throws NoSuchElementException |
Returns an empty list |
WebElement element = driver.findElement(By.xpath("value of xpath"));
List<WebElement> links =
driver.findElements(By.xpath("value of xpath"));
Script to Count the Number of Links on a Page
Links are represented by <a> tags.
Find all anchor tags and use the list size.
public class LinkCountScript {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
java.util.List<WebElement> linkElements =
driver.findElements(By.tagName("a"));
int linkCount = linkElements.size();
System.out.println(
"Number of links on the page: " + linkCount);
driver.quit();
}
}
Getting the Current URL and Page Source
getCurrentUrl()
Returns the URL of the current page.
String currentURL = driver.getCurrentUrl();
System.out.println("Current URL: " + currentURL);
getPageSource()
Retrieves the complete HTML source code of the current page.
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
String pageSource = driver.getPageSource();
System.out.println(pageSource);
Uses
-
Verify page content.
-
Check whether text exists on the page.
-
Debugging.
Browser Navigation, Maximize, and Minimize
Browser Navigation
driver.navigate().back();
driver.navigate().forward();
Maximize Browser
driver.manage().window().maximize();
Minimize Browser
driver.manage().window().minimize();
Note
-
manage()returns the Options interface. -
window()returns the Window interface.
Therefore, the method chain becomes:
driver.manage().window().maximize();
Checking Element State
isEnabled()
Returns whether the element is enabled.
-
true -
false
isDisplayed()
Returns whether the element is currently visible on the page.
isSelected()
Returns whether:
-
Checkbox
-
Radio button
-
Option
is selected.
boolean isEnabled = buttonElement.isEnabled();
if (isEnabled) {
buttonElement.click();
}
Getting Location, Size, and CSS Values
getLocation()
Returns a Point object.
Use getX() and getY() to retrieve coordinates.
Point location = element.getLocation();
int x = location.getX();
int y = location.getY();
getCssValue()
Retrieves CSS properties.
Examples include:
-
Color
-
Height
-
Width
-
Font Size
String color = element.getCssValue("color");
getSize()
Returns a Dimension object.
From it you can retrieve:
-
Height
-
Width
close() vs quit()
close()
Closes the current browser window.
driver.close();
quit()
Closes all browser windows associated with the current WebDriver session.
driver.quit();
Difference
| close() | quit() |
|---|---|
| Closes only the current browser window | Closes all browser windows and ends the WebDriver session |
Use:
-
close() when only the active window should be closed.
-
quit() at the end of the test to terminate the complete WebDriver session.
FAQs
What methods does SearchContext provide?
findElement() and findElements().
These methods are inherited by both WebDriver and WebElement.
What is the difference between get() and navigate()?
-
get()loads the page and waits until it is fully loaded. -
navigate().to()belongs to the Navigation interface and also supports Back, Forward, and Refresh.
What is the difference between findElement() and findElements()?
-
findElement()returns the first matching element and throwsNoSuchElementExceptionif none is found. -
findElements()returns a list of all matching elements and returns an empty list when none are found.
How do you count the number of links on a page?
Find all <a> tags using:
driver.findElements(By.tagName("a"))
Then call:
.size()
What does getPageSource() do?
Returns the complete HTML source code of the current page as a string.
Useful for:
-
Verification
-
Debugging
How do you maximize the browser?
driver.manage().window().maximize();
manage() returns the Options interface, and window() returns the Window interface.
What is the difference between close() and quit()?
-
close()closes the current browser window. -
quit()closes every browser window and ends the WebDriver session.
How do you check whether an element is visible, enabled, or selected?
Use:
-
isDisplayed() -
isEnabled() -
isSelected()
Each method returns a boolean value.