WebDriver Interface Methods
WebDriver Interface Methods
WebDriver is a key component of Selenium. The WebDriver Interface is a part of the WebDriver API, which is used for automating web application testing. It provides a set of methods for interacting with a web browser programmatically.
Characteristics of WebDriver Interface
Browser Control: WebDriver allows you to control a browser by sending commands like navigating to URLs, clicking on elements, filling out forms, taking screenshots, and many others.
Platform Independence: WebDriver interacts with browsers directly, making it platform-independent. You can use WebDriver to automate browsers on different operating systems like Windows, macOS, and Linux.
Supports Multiple Browsers: WebDriver can work with different browsers (e.g., Google Chrome, Mozilla Firefox, Microsoft Edge, Apple Safari, etc.) through browser-specific drivers. Each browser has its own WebDriver implementation (like ChromeDriver for Chrome, GeckoDriver for Firefox, etc.).
Direct Interaction: Unlike other automation tools like Selenium RC, WebDriver interacts directly with the browser, making it faster and more efficient. It simulates user actions like clicking, typing, and selecting elements.
Automation of User Interactions: It allows automation of various user interactions such as navigating, clicking on buttons, submitting forms, waiting for elements to appear, retrieving text, and handling alerts, pop-ups, etc.
WebDriver Methods
Method | Description |
---|---|
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">get()</span> |
Navigate to a specified URL in the browser. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getTitle()</span> |
Get the title of the current page. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getCurrentUrl()</span> |
Get the URL of the current page. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getPageSource()</span> |
Get the entire HTML source of the current page. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">findElement()</span> |
Locate an element on the page using a locator (e.g., CSS, XPath). |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">findElements()</span> |
Locate multiple elements on the page using a locator. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">click()</span> |
Click an element. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">sendKeys()</span> |
Simulate typing into an input field. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getText()</span> |
Get the visible text of an element. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getAttribute()</span> |
Get the value of an attribute of an element. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getWindowHandle()</span> |
Get the handle of the current browser window. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">getWindowHandles()</span> |
Get the handles of all browser windows. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">switchTo().window()</span> |
Switch the context to another window. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">quit()</span> |
Close the browser and end the WebDriver session. |
<span style="color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;">close()</span> |
Close the current browser window. |
Example
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverExample {
public static void main(String[] args) {
// Set up ChromeDriver (assumes chromedriver is installed)
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create a WebDriver instance
WebDriver driver = new ChromeDriver();
// Navigate to a website
driver.get("https://www.example.com");
// Get and print the page title
String title = driver.getTitle();
System.out.println("Page Title: " + title);
// Close the browser
driver.quit();
}
}