Brower Commands

1. get(String url) command

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.

Method:

driver.get(“https://www.w3wiki.org/”);

or

String URL = “https://www.w3wiki.org/”;

driver.get(URL);

Output:

Explanation:

  1. This command will start the loading of a new web page in the web browser that is currently open.
  2. It takes one argument in the form of a string which contains the URL of the page desired to be retrieved.
  3. This method is identical to the driver.navigate().to() which works for the same purpose.
  4. But, the driver.get() method waits until the page is completely finished loading before returning control back to the script whereas driver.navigate().the to() does not wait for the page to load and terminates quickly.

2. getTitle() Command

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.

Method:

String pageTitle = driver.getTitle();

System.out.println(pageTitle);//it will print the title of the specified URL.

Output:

Explanation:

  1. This command obtains the title of the active webpage which will be same as displayed in the browser.
  2. It does not take any arguments and it return the title of the specified URL as a string.
  3. It can be utilized to access the title of the URL, which is be convenient for the purpose of confirmation in automated tests and for logging as well as reporting.

3. getCurrentUrl() Command

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.

Method:

String currentURL = driver.getCurrentUrl();

Output:

Explanation:

  1. This technique obtains the web address of the present page.
  2. No arguments are accepted and it return the URL of the webpage as a string.
  3. This technique is similar to clicking within the search field of the browser and copying the link from the search area.
  4. It can be utilized to validate the present URL, which is very expedient for the function of corroboration in automated tests and for logging as well as reporting.

4. getPageSource() Command

It gets the entire page source of the current web page loaded in the browser. It does not accept any parameters but it do returns the page source as a string.

Method:

String pageSource = driver.getPageSource();

Output:

Explanation:

  1. This technique returns the entirety of the page source code of the existing URL.
  2. Taking in no parameters, it returns the page source as a string.
  3. This method corresponds to clicking anywhere on the current tab or browser window then picking ‘View page source’ or hitting CTRL + U on the keyboard.
  4. It’s mostly used for extracting data (web scraping), diagnosing issues, and verifying page content.

5. close() Command

It closes the current browser window or tab. Also, this command does not accept any types of parameters. It also does not return anything.

Method:

driver.close();

Output:

Explanation:

  1. This approach shuts down the present window or tab of the browser.
  2. No parameters are accepted and nothing is returned.
  3. The said method is equal to clicking on the X of the active tabs or pressing CTRL + W on the keyboard.
  4. It is available to be utilized for closing specific browser windows or tabs while operating with several tabs in a single WebDriver session.

6. quit() Command

It closes all the browser windows and tabs for a particular WebDriver session. This command does not accept any parameters and not return anything.

Method:

driver.quit();

Output:

Explanation:

  1. This method quits the WebDriver session, closing all open browser windows and tabs associated with that session.
  2. It does not accept any parameters and does not return anything.
  3. This method is equivalent to clicking on the cross mark present in the top rightmost corner or pressing the ALT + F4 key on the keyboard.
  4. It can be used to end a WebDriver session and release browser resources.

Selenium WebDriver – Browser Commands

Selenium WebDriver is an extremely useful tool for automating web applications and browser interactions. Through its collection of browser commands, developers and testers can control web browsers in a programmatic fashion. Browser commands that enable tasks such as web scraping, simplifying everyday operations on the web, and testing web applications. As Selenium WebDriver is compatible with a range of programming languages, it is highly accessible among developers of varying levels. The article focuses on discussing Selenium WebDriver Browser Commands in detail.

Similar Reads

What are Browser Commands?

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

Prerequisites

1. Java Development Kit (JDK): Install the JDK to write and execute Java code....

Brower Commands

1. get(String url) command...

Implementation

Interaction with the GeeksForGeeks Website using Brower Commands:...

Conclusion

...

Contact Us