While working with Selenium we need to use Java as well as Selenium WebDriver functions. WebDriver provides various methods to perform different actions on Web elements.
Common Selenium WebDriver Methods
Common elements methods for UI interaction
The following lists show details of some of the methods that selenium provides to perform actions on different web elements (listbox, editbox, checkbox, radio button) depending upon their type.
Method | Purpose |
Clear() | Clears all of the contents if the elements is a text entity. |
Click() | Simulates a mouse click on the element. |
getAttribute(String Name) | Returns the value associated with the provided attribute name if present or null if not present. |
getTagName() | Returns the tag name of element. |
getText() | Returns the visible text contained within this element (including sub elements) if not hidden via CSS. |
getValue() | Gets the value of the element’s “value” attribute. |
isEnabled() | Returns true for input elements that are currently enabled otherwise false. |
isSelected() | Returns true if the element (like radio button, check box) is currently selected, otherwise false. |
sendKeys(CharSequence…) | Simulates typing into an element. |
setSelected() | Select an element (like radio button, check box, options within a select) |
Submit() | Submits the same block if the elements is a form or contained within a form. Blocks until new page is loaded. |
toggle() | Toggles the state of a checkbox element. |
getAttribute is one of the most common methods used specifically to get property values and used to verify data in the fields.
Search elements methods
WebDriver also provides two methods allowing you to search for elements within the current page’s scope:
Method | Purpose |
findElement(By by) | Finds the first element located by the provided method (based on different location type) |
findElements(By by) | Finds all elements located by the provided method |
Select web elements methods
WebDriver provides a support class named Select to simplify interaction with select elements and their association options. It is mostly used with elements of type list boxes.
Method | Purpose |
selectByIndex(int index)/
deselectByIndex(int index) |
Select/deselect the option at the given index |
selectByValue(String value) /
deselectByValue(String value) |
Select/ deselect the option that has a value matching the argument. |
selectByVisibleText(String text)/
deselectByVisibleText(String text) |
Select/deselect the option that displays text matching the argument. |
deselectAll() | Deselects all options |
getAllSelectedOptions() | Returns a List<WebElement> of all selected options |
getFirstSelectedOption() | Returns a WebElement representing the first selected option. |
getOptions() | Returns a List<WebElement> of all options |
isMultiple() | Returns true if this is a multi-select list, otherwise false |
Interacting with Rendered Elements
If you are driving an actual browser such as Firefox, you also can access a fair amount of information about the rendered state of an element by casting it to RenderedWebElement. This is also how you can simulate mouse-hover events and perform drag-and-drop operations.
WebElement element = driver.findElement(By.id(“header”));
RenderedWebElement renderedElement = (RenderedWebElement) element;
RenderedWebElement Methods
Method | Purpose |
dragAndDropBy(int moveRightBy, int moveDownBy) | Drags and drops the elements moveRightBy pixels to the right and moveDownBy pixels down. Pass negative arguments to move left and up. |
dragAndDropOn(RenderedWebElement element) | Drags and drops the element on the supplied elemnt. |
getLocation() | Returns a Java.awt.Point representing the top left-hand corner of the element. |
getSize() | Returns a Java.awt.Dimension representing the width and height of the element. |
getValueOfCssProperty(String propertyName) | Returns the value of the provided property. |
hover() | Simulates a mouse hover event over the element. |
isDisplayed() | Returns true if the element is currently displayed, otherwise false. |
WebDriver Methods
WebDriver methods are useful when you are working with the browser object itself and you would want to perform certain operations like close browser, get title of browser page, or if working with application which has multiple frames or web pop-up windows.
Method | Purpose |
close() | Close the current window, quitting the browser if it’s the last window currently open. |
get(Java.lang.String,url) | Load a new Web page in the current browser window. |
getCurrentUrl() | Gets a string representing the current URL that the browser is looking at. |
getPageSource() | Get the source of the last loaded page. |
getTitle() | The title of the current page. |
getWindowHandle() | Return an opaque to this window that uniquely identifies it within this driver instance. |
getWindowHandles() | Return a set of window handles which can be used to iterate over all open windows of this webdriver instance by passing them to switchTo().WebDriver.Options.window() |
manage() | Gets the Option interface |
navigate() | An abstraction allowing the driver to access the browser’s history and to navigate to a given URL. |
quit() | Quits this driver, closing every associated window. |
Leave a Reply Cancel reply