☰ See All Chapters |
Selenium WebDriver methods in Java
In this tutorial we are listing all the important methods used in selenium java webdriver. In upcoming chapters you will see the detailed usage of these some of these methods along with the examples. In this chapter it is provided with a short description of these methods.
Methods of SearchContext interface
Method | Description |
findElement() | The “findElement()” method returns the first WebElement object based on a specified search criteria, or throws an exception, if it does not find any element matching the search criteria. Throw a “NoSuchElementFoundException” exception when they fail to find the desired element using the specified locator strategy. |
findElements() | The “findElements()” method returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list. Throw a “NoSuchElementFoundException” exception when they fail to find the desired element using the specified locator strategy. |
Methods of WebDriver interface
Method | Description |
close() | It closes the current window. |
get() | It is used to open an URL and it will wait till the whole page gets loaded. WebDriver will wait until the page has fully loaded before returning control to your test or script. |
getTitle() | It returns the title of current page/window |
getPageSource() | This method gets the text of a whole page, for example: if(driver.getPageSource().contains("Whatever you look for")) { ... } |
getCurrentUrl() | Used to get the URL of the currently opened web page in the browser. |
getWindowHandle() | This method gives the handle of the page the webDriver is currently controlling. This handle is a unique identifier for the web page. This is different every time you open a page even if it is the same URL. |
getWindowHandles() | This method gives all the handles for all the pages that the web driver understands in the order that they have been opened. |
manage() | This method is used to manage driver like setting timeout, adding cookies etc.. Example: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().addCookie(CookieObjectName); driver.manage().getCookies(); |
navigate() | Navigate is one such feature of WebDriver that allows the test script developer to work with the browser's “Back”, “Forward”, and “Refresh” controls of the browser. The method that is used for this purpose is “navigate()”, for example: driver.navigate().back(); driver.navigate().forward(); driver.navigate().refresh(); |
quit() | quit() method is used to terminate the session. This will closes all the browser windows. |
switchTo() | This method is used to switch between the windows. |
Methods of TakesScreenshot interface
Method | Description |
getScreenshotAs() | This method is used to capture the screenshots during execution. |
Methods of JavascriptExecutor interface
Method | Description |
executeScript() | JavaScriptExecutor is used to execute JavaScript through Selenium Webdriver. |
executeAsyncScript() | We don’t use this for automation |
Methods of WebElement interface
Method | Description |
clear() | It is used to clear any value which is present in any element (eg: text box,text area etc) |
click() | It is used to click on Buttons and Links (and other web elements). |
getAttribute() | It is used to the get the value of the attribute in the form of string. As an argument to this method we pass the attribute name in the form of string. When we pass an attribute name which is not present in the html source code of an element as an argument to this method, it returns an empty “ ”string. |
getCssValue() | It is used to the get the value of the specified style attribute in the form of string. Example: String width = message.getCssValue("width"); |
getLocation() | getLocation() is a method present in WebElement interface. It returns an instance of Point class. Point class has few non static methods like getX() and getY(). getX() method returns the coordinate of an element. getY() method returns the y coordinate of an element. |
getRect() | It returns a Rectagle instance. Rectangle class provides getter methods to get the width, height and location of the given web element. Example: WebElement element = driver.findElement(By.id("header1")); Rectangle rectangle = element.getRect(); rectangle.getWidth(); rectangle.getHeight(); rectangle.getX(); rectangle.getY(); |
getSize() | getSize() is a method present in WebElement interface. It returns an instance of Dimension class. Dimension class has few non static methods like getHeight() and getWidth(). getHeight() method returns the height of an element. getWidth() method returns the width of an element. Example: WebElement element = driver.findElement(By.id("header1")); Dimension dimension = element.getSize(); dimension.getWidth(); dimension.getHeight(); |
getTagName() | It is use to get the tagname of an element. It doesn’t accept any argument. |
getText() | It is use to get the text present in an element. It doesn’t accept any argument. When there is no text present in the html source code of an element and we are using getText() method to retrieve the text of an element, it returns an empty string. |
isDisplayed() | isDisplayed() is a method present in WebElement interface. It checks whether an element is present or not on the webpage. If the element is present on the webpage, it returns true. And if the element is not present on the webpage, it returns false. |
isEnabled() | isEnabled() is a method present in WebElement interface. It checks whether an element is enabled or not on the webpage. If the element is enabled on the webpage, it returns true. And if the element is disabled on the webpage, it returns false. |
isSelected() | isSelected() is a method present in WebElement interface. It checks whether an element is selected or not on the webpage. If the element is selected on the webpage, it returns true. And if the element is not selected on the webpage, it returns false. |
sendKeys() | sendkeys() is a method present in WebElement interface. It is used to enter any value in an element (eg: text box, text area etc) |
submit() | We can use submit method to click on an element if the element is present inside a form(tag) and html source code of the element has an attribute called type = “submit”. When both the conditions satisfied we use submit() method. |
All Chapters