top of page

Selenium Web Driver Commands

pavithrasubburaj

Selenium Web Driver

• Selenium WebDriver is a powerful tool for automating the web browser to perform certain tasks. Selenium supports multiple browsers (such as Chrome, Firefox, Edge, Safari, etc.) and multiple programming languages (such as Java, Python, C#, etc.) so, it is very easy to use and automate tasks on a browser.

• Selenium WebDriver provides predefined methods and functions that enable developers to interact directly with browsers.

• By using WebDriver commands, you can automate various browser tasks such as navigating between browser tabs or windows, clicking on buttons or particular elements on web pages, performing actions on web elements, and other day-to-day tasks.

What are Selenium WebDriver Commands?

• Selenium WebDriver commands are set of functions and method used for controlling or automating the web browser

• These command helps developer and tester to write script programmatically using various languages (Java, C#, Python, etc.) to interact with web elements or perform various automation task.

• These set of command is core of the Selenium WebDriver.

These Commands are classified into three categories: -

1. Browser commands

2. Navigation commands

3. WebElement commands


Selenium WebDriver - Browser Commands


• Browser commands provide exact control over the browser's actions, like getting specific pages, extracting information such as page titles and URLs, accessing page source code, and controlling browser windows.

• Browser commands provide an easy way to interact with web applications and to perform automation and scraping data from web pages.

Methods:

Browser Commands

The Browser Command provides 6 methods: get(), getTitle(), getCurrentUrl(), getPageSource() , close() and quit().

Command

Example

Explanation

1

get()

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

It loads a new web page in the current browser window and accepts a string parameter that specifies the URL of the web page to be loaded.

2

getTitle()

driver.getTitle(); 

It gets the title of the current web page displayed in the browser. It does not accept any parameters.

It returns the title of the specified URL as a string.

3

getCurrentUrl()

driver.getCurrentUrl();

It gets the URL of the current web page shown in the browser.

It does not accept any parameters and returns the URL as a string.

4

getPageSource

driver.getPageSource();

It gets the entire page source of the current web page loaded in the browser.

It does not accept any parameters, but it does return the page source as a string.

 

5

close()

driver.close();

It closes the current browser window or tab.

Also, this command does not accept any types of parameters.

It also does not return anything.

6

quit()

driver.quit();

It closes all the browser windows and tabs for a particular WebDriver session.

This command does not accept any parameters and not return anything.

Example :

Selenium WebDriver - Navigation Commands


• Navigation commands in Selenium WebDriver perform operations that involve navigating through web pages and controlling browser behavior.

• These commands provide an efficient way to manage a browser's history and perform actions like going back and forward between pages and refreshing the current page.

Methods:

Navigation commands

The Navigation Command provides four methods: to(), back(), forward(), and refresh(). These methods allow the WebDriver to perform the following below operations:


1. to() Command

Loads a new web page in the current browser window. It accepts a string parameter that specifies the URL of the web page to be loaded.

Example:

driver.navigate().to("https://www.google.com/");

Explanation:

  • This method loads a new web page in the current browser window.

  • It accepts a string parameter that specifies the URL of the web page to be loaded.

  • This method is equivalent to the driver.get(“https://www.google.com”) method, which also loads a new web page in the current browser window.

Difference between get() and navigate.to() command

  • However, the driver.get() method waits for the page to load completely before returning control to the script, while the driver.navigate().to() method does not wait for the page to load and returns immediately.

  • Therefore, the driver.navigate().to() method is faster than the driver.get() method, but it may cause synchronization issues if the script tries to interact with elements that are not yet loaded on the page.


2. back() Command

Moves back one step in the browser’s history stack. It does not accept any parameters and does not return anything.

Example:

driver.navigate().back();

Explanation:

  • This method moves back one step in the browser’s history stack.

  • It does not accept any parameters and does not return anything.

  • This method is equivalent to clicking on the back button of the browser.

  • It can be used to return to the previous web page that was visited in the current browser session.


3. forward() Command

Moves forward one step in the browser’s history stack. It does not accept any parameters and does not return anything.

Example:

driver.navigate().forward();

Explanation:

  • This method moves forward one step in the browser’s history stack.

  • It does not accept any parameters and does not return anything.

  • This method is equivalent to clicking on the forward button of the browser.

  • It can be used to go to the next web page that was visited in the current browser session after using the back() method.


4. refresh() Command

Reloads the current web page in the browser window. It does not accept any parameters and does not return anything.

Example:

driver.navigate().refresh();

Explanation:

  • This method reloads the current web page in the browser window.

  • It does not accept any parameters and does not return anything.

  • This method is equivalent to clicking on the refresh button of the browser or pressing F5 key on the keyboard.

  • It can be used to refresh the content of the web page or to retry a failed operation.


Example:

WebElement commands:

• To interact with various web element attributes such as (buttons, text fields, links, checkboxes, radio buttons, and more.)

• selenium webdriver provide a way to interact with all the webelement present on the web page and manipulate them by using Webelement commands.

• With help of these commands developer or tester can perform various tasks like typing on text field, clicking the button, reading text values, checking and unchecking radio buttons, selecting item from dropdowns etc.


WebElement Command

The WebElement Commands provide method: sendKeys(), isDisplayed(), isSelected(), submit(), isEnabled(), getLocation(), clear(), getAttribute(), getText(), getTagName(), click() etc.


1.SendKeys() command:

• Enter text automatically into editable field while executing tests. these field are identified using locators like element id, name, class name, etc.

Example:

WEBELEMENT.sendkeys(“text”);


2.isDisplayed() command:

• It verifies whether a web element is present and visible on the web page.

• Returns true if the element is displayed and false if not.

Example:

WEBELEMENT.isDisplayed();


3.isSelected() command:

• It used on radio, checkboxes, dropdowns to check whether element is selected or not.

• If the specified element is selected, the value returned is true.

• If not, the value returned is false.

Example:

WEBELEMENT.isSelected();


4.submit() command:

• It used to submit forms on browser. It doesn’t require a parameter and returns nothing.

Example:

WEBELEMENT.submit();


5.isEnabled() command:

• Used to checks if an element is enabled for interaction on the web page or not.

• It returns true if the element is enabled and false if not.

Example:

WEBELEMENT.isEnabled();


6.getLocation() command:

• Retrieves the location of a specific web element on the page in terms of its X and Y coordinates.

Example:

WEBELEMENT.getLocation();


7.clear() command:

• Used to Clears the content of text entry fields or text areas.

• It doesn’t require a parameter and returns nothing.

Example:

WEBELEMENT.clear();


8.getSize() command:

• Used to retrieves the height and width of a rendered element on the web page.

Example:

WEBELEMENT.getSize();


9.getAttribute() command:

• Used to retrieves the value of a specified attribute of a web element.

Example:

WEBELEMENT.getAttribute();


10.click() command:

• Used to perform click operation on a web element such as a button, link, or checkbox

Example:

WEBELEMENT.click();

WebElements command Example 1
WebElements Example 2

68 views
bottom of page