Selenium Webdriver Tutorials

driver.get in Selenium WebDriver

After successfully running several automation scripts now we are stepping towards grasping the essential ‘Get’ commands in Selenium. We will precisely and briefly discuss each of these Selenium commands so as to make you capable of using them effectively whenever required. Thus we are going to discuss about various driver.get in Selenium WebDriver that we would be using in our day to day automation testing. driver.get in Selenium WebDriver are used to collect various information about the page that we deal with. Here are some important “get” commands you must be familiar with,

driver.get in Selenium WebDriver

driver.get in Selenium WebDriver

get(String arg0) : void – WebDriver

The first thing you’ll want to do with WebDriver is to open a web page. Called using the WebDriver instance, get() command is used to open a new browser window and find/fetch the web page given as the argument. Webdriver will wait until the page has fully loaded before returning the control to test or script.

  • arg0 – The URL to load
  • Return type: Void (it returns nothing)
  • Called using: WebDriver instance

loads a new web page in the current browser window.

Note: It is always best to use a fully qualified URL, i.e. starting with http: instead of www. Never forget to give http, else Selenium throws the below run-time error. However “www” is not mandatory.

Exception in thread “main” org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {“code”:-32603,”message”:”Cannot navigate to invalid URL”}

getClass() : Class<?> – Object

getClass( ) method is one of the Object Class methods. As all the other Classes in Java are the sub classes of the Object Class, any Class can access this method using its object. This method fetches the runtime class of the calling object.

  • No argument
  • Return type: Class of the calling instance/object (usually web element)
  • Called using: Any class object

Result: class org.openqa.selenium.chrome.ChromeDriver

getCurrentURL() : String – WebDriver

This method fetches the string representing the Current URL which is opened in the browser. Accepts nothing as a parameter and returns a String value. As the return type is String value, the output must be stored in String object/variable.

  • No argument
  • Return type: String representation of current URL
  • Called using: WebDriver instance

Result: https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier

getPageSource() : String – WebDriver

This method returns the Source Code of the page. The command can be used with various string operations like contains() to ascertain the presence of the specified string value,

Accepts nothing as a parameter and returns a String value. As the return type is String value, the output must be stored in String object/variable.

  • No argument
  • Return type: String representation of current web page’s source
  • Called using: WebDriver instance

Result: Complete Page source for the current web page

getTitle() : String – WebDriver

This command is used to get the title of the current page. As the return type is String value, the output must be stored in String object/variable.

  • No argument
  • Return type: String representation of ‘Title’ of the current web page
  • Called using: WebDriver instance

Result: Gmail

getWindowHandle() : String – WebDriver

This command is used when we have more than one window to deal with. It helps automation to switch to the newly opened window and perform actions on the new window. Script can then switch back to the previous window whenever required.

  • No argument
  • Return type: String representation or handle to the currently opened window
  • Called using: WebDriver instance

getWindowHandles() : Set<String> – WebDriver

As the name suggests, it is similar to ‘getWindowHandle()’ with the subtle difference that it helps to deal with ‘multiple’ windows i.e. more than 2 windows.

  • No argument
  • Return type: A set of String values, representing the handle to the currently opened window’s’
  • Called using: WebDriver instance

Hope you got a fair idea about driver.get in Selenium WebDriver? If yes, don’t forget to share the article with your colleagues & friends!

Leave a Reply

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