WebDriver Methods

Selenium WebDriver methods are used to perform operations on Browser and Web Elements. When we get the driver object, the below are the methods that we can perform operation on a driver. In IDE like eclipse, when we add period (. ) you will show all the following methods.

Method Name :- get()

Syntax: get(url)

Example: driver.get(“http://facebook.com”);

Purpose: It will load a new web page in the current browser window.

Method Name: getCurrentUrl()

Syntax: getCurrentUrl()

Example: String CurrentUrl = driver.getCurrentUrl();

System.out.println(CurrentUrl);

Purpose: Gets a string representing the current URL that the browser is opened.

Returns: https://www.facebook.com/

Method Name: getTitle()

Syntax: getTitle()

Example: String Title = driver.getTitle();

System.out.println(Title);

Purpose: Gets the title of the current web page.

Returns: Facebook – log in or sign up

Method Name: findElements()

Syntax: findElements(By by)

Example: driver.findElements(By.id(“email“);

Purpose: Find all elements within the current page using the given mechanism.

Parameters: By – The locating mechanism to use

Returns: A list of all WebElements, or an empty list if nothing matches

Method Name: findElement()

Syntax: WebElement findElement(By by)

Example: driver.findElements(By.xpath(“//”);

Purpose: Find the first WebElement using the given method.

Parameters: By – The locating mechanism

Returns: The first matching element on the current page

Throws: NoSuchElementException – it will return exception if no matching elements are found

Method Name: getPageSource()

Syntax: getPageSource()

Example: String PageSource = driver.getPageSource();

System.out.println(PageSource);

Purpose: Get the source of the currently loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page.

Returns: The source of the current page

Method Name: close()

Syntax: void close()

Example: driver.close();

Purpose: Close the current window, if there are multiple windows, it will close the current window which is active and quits the browser if it’s the last window opened currently.

Method Name: quit()

Syntax: void quit()

Example: driver.quit();

Purpose: Quits this driver instance, closing every associated window which is opened.

Method Name: getWindowHandles()

Syntax: Set getWindowHandles()
Example: driver.getWindowHandles();
Purpose: Return a set of window handles which can be used to iterate over all the open windows of this Webdriver instance by passing them to switchTo().WebDriver.Options.window()
Returns: A set of window handles which can be used to iterate over all the open windows.

Method Name: getWindowHandle()

Syntax: String getWindowHandle()

Example: driver.getWindowHandle();

Parameter: Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date

switchTo

WebDriver.TargetLocator switchTo()

The next future commands will be performed to a different frame or window.

Returns: A Target Locator which can be used to switch or select a frame or window

Method Name: navigate().to()

Loads a new web page in the current browser window.

Syntax: driverObject.navigate().to(“url”)

Example:

driver.get(“https://google.com/”);

driver.navigate().to(“https://yahoo.com/”);

Method Name: navigate().back()

It moves a single item back in the browser history.

Syntax: driverObject.navigate().back()

Example: driver.get(“https://facebook.com/“);

driver.navigate().to(“https://gmail.com/”);

driver.navigate().back();

Method Name: navigate().forward()

It moves a single item forward in the browser history.

Syntax: driverObject. navigate().forward()

Example: driver.get(“https://google.com/”);

driver.navigate().to(“https://gmail.com/“);

driver.navigate().back();

driver.navigate().forward();

Method Name: navigate().refresh()

It will refresh the current web page

Syntax: driverObject.navigate().refresh()

Example: driver.get(“https://google.com/”);

driver.navigate().refresh();

Method Name: manage()

Syntax: WebDriver.Options manage()

Purpose: Gets the Option interface

Returns: An option interface

Method Name: sendkeys()

Enters a value into text box

Syntax: driverObject.findelement(By.id(“Emelemt Id”).sendkeys(“text”);

Example: driver.findElement(By.id(“Email”)).sendKeys(“hello@yopmail.com”);

Or

WebElement text = driver.findElement(By.id(“Email”));

text.sendKeys(“hello@yopmail.com”);

Method Name: clear()

It clears the value from an Edit box

Syntax: driverObject.findelement(By.locator(“value”).clear();

Example: WebElement editbox = driver.findElement(By.id(“Email”));

editbox.sendKeys(“hello@yopmail.com”);

editbox.clear();

Or

driver.findElement(By.id(“Email”)).sendKeys(“hello@yopmail.com”);

driver.findElement(By.id(“Email”)).clear();

Method Name: click()

It clicks an element

Syntax: driverObject.findelement(By.locator(“value”).click;

Example: driver.findElement(By.id(“next”)).click();

Method Name: isEnabled()

it checks weather the element is in enabled state or not?

Syntax: boolean variableName = driverObject.findelement(By.locator(“value”).isEnabled();

Example: boolean a = driver.findElement(By.id(“next”)).isEnabled();

System.out.println(a); //true

Method Name: isDisplayed()

It checks if the element is displayed or not?

Syntax: boolean variableName = driverObject.findelement(By.locator(“value”).isDisplayed();

Example: boolean a = driver.findElement(By.id(“next”)).isDisplayed();

System.out.println(a); //true

Method Name: manage().window().maximize()

Syntax: driver.manage().window().maximize();

Example: driver.get(“https://gmail.com/”);

driver.manage().window().maximize();

Method Name: isSelected()

It checks if the check box or Radio button is selected or not?

Syntax:

boolean variableName = driverObject.findelement(By.locator(“value”).isDisplayed();

Leave a Reply

Your email address will not be published. Required fields are marked *