- Jun 06, 2018
- admin
- 0
Here are some of the WebDriver common commands used to access elements, browser navigation, getting browser or elements information.
Instantiating Web Elements
Before performing any action on any WebElement, we need to Instantiate that WebElement. We can use “driver.findElement(By.locator())” syntax to find any WebElement, but Instead of using the long “driver.findElement(By.locator())” syntax every time you will access a particular element, we can instantiate a WebElement object for it. The WebElement class is contained in the “using OpenQA.Selenium” name space.
IWebElement firstName=driver.FindElement(By.Id(“FirstName”));
firstName.SendKeys(“TechTutorialz”);
Clicking on an Web Element
Click event is perhaps the most common way of interacting with web elements. The click() method is used to simulate the clicking of any element. T
Following example shows how click() was used to click on “Google Search” button from Google.com Home page.
driver.FindElement(By.Name(“btnK”)).click();
before using click() method on any element, we need to make sure the element to be clicked on is visible. Click() method wont take any parameters.
Get Commands
Get commands fetch the important information about the page/element. Here are some of the important “get” commands..
WebDriver command | Usage |
---|---|
get() | • The command launches a new browser and opens the specified URL in the browser instance • The command takes a single string type parameter that is usually a URL of application under test • To the Selenium IDE users, the command may look very much like open commanddriver.get(“https://google.com”); |
getClass() | The command is used to retrieve the Class object that represents the runtime class of this object driver.getClass(); |
getCurrentUrl() | • The command is used to retrieve the URL of the webpage the user is currently accessing • The command doesn’t require any parameter and returns a string valuedriver.getCurrentUrl(); |
getPageSource() | • The command is used to retrieve the page source of the webpage the user is currently accessing • The command doesn’t require any parameter and returns a string value • The command can be used with various string operations like contains() to ascertain the presence of the specified string valueboolean result = driver.getPageSource().contains(“String to find”); |
getTitle() | • The command is used to retrieve the title of the webpage the user is currently working on. A null string is returned if the webpage has no title • The command doesn’t require any parameter and returns a trimmed string valueString title = driver.getTitle(); |
getText() | • The command is used to retrieve the inner text of the specified web element • The command doesn’t require any parameter and returns a string value • It is also one of the extensively used commands for verification of messages, labels, errors etc displayed on the web pages.String Text = driver.findElement(By.id(“Text”)).getText(); |
getAttribute() | • The command is used to retrieve the value of the specified attribute • The command requires a single string parameter that refers to an attribute whose value we aspire to know and returns a string value as a result. driver.findElement(By.id(“findID”)). getAttribute(“value”); |
getWindowHandle() | • The command is used to tackle with the situation when we have more than one window to deal with. • The command helps us switch to the newly opened window and performs actions on the new window. The user can also switch back to the previous window if he/she desires. private String winHandleBefore; winHandleBefore = driver.getWindowHandle(); driver.switchTo().window(winHandleBefore); |
getWindowHandles() | • The command is similar to that of “getWindowHandle()” with the subtle difference that it helps to deal with multiple windows i.e. when we have to deal with more than 2 windows. |
Browser Navigation commands
These navigation commands allow you to refresh,go-into and switch back and forth between different web pages.
navigate().to() |
|
navigate().refresh() |
|
navigate().back() |
|
navigate().forward() |
|
Closing and Quitting Browser Windows
driver.Close();
Close() method don’t take any parameters.
It closes only the browser window that WebDriver is currently controlling.
driver.Quit();
Quit() method don’t take any parameters.
It closes all browser instances/windows that WebDriver has opened.
If there are any pop-ups opened on top the parent window, quit() method will close the pop-ups also along with parent window.