Skip to main content

Command Palette

Search for a command to run...

Selenium-Java All Methods CheatSheet

Published
โ€ข5 min readโ€ขView as Markdown

1. WebDriver Interface Methods

MethodReturn TypeDescription
get(String url)voidOpens a URL in the browser
getCurrentUrl()StringReturns the current page URL
getTitle()StringReturns the page title
getPageSource()StringReturns the HTML source of the page
close()voidCloses the current browser window
quit()voidQuits all browser windows and ends session
getWindowHandle()StringReturns the handle of the current window
getWindowHandles()Set<String>Returns a set of all open window handles
switchTo()TargetLocatorSwitches to frame, window, or alert
navigate()NavigationProvides browser navigation options
manage()OptionsProvides browser options like cookies, timeouts

๐Ÿ”น 2. WebElement Interface Methods

MethodReturn TypeDescription
click()voidClicks the element
submit()voidSubmits the form element
sendKeys(CharSequence...)voidTypes into the element
clear()voidClears the value of input field
getTagName()StringGets the tag name of the element
getAttribute(String name)StringGets the attribute value
getText()StringGets the visible text
getCssValue(String propertyName)StringGets CSS property value
getLocation()PointReturns location of the element
getSize()DimensionGets size of the element
getRect()RectangleGets dimensions and position
isDisplayed()booleanChecks if visible
isEnabled()booleanChecks if enabled
isSelected()booleanChecks if selected
findElement(By by)WebElementFinds child element
findElements(By by)List<WebElement>Finds list of child elements

๐Ÿ”น 3. Navigation Interface (from driver.navigate())

MethodReturn TypeDescription
to(String url)voidNavigates to a URL
to(URL url)voidNavigates to a URL object
back()voidNavigates back in browser history
forward()voidNavigates forward
refresh()voidReloads the current page

๐Ÿ”น 4. TargetLocator Interface (from driver.switchTo())

MethodReturn TypeDescription
frame(int index)WebDriverSwitch to frame by index
frame(String nameOrId)WebDriverSwitch to frame by name or ID
frame(WebElement element)WebDriverSwitch to frame by WebElement
defaultContent()WebDriverSwitch back to main frame
parentFrame()WebDriverSwitch to parent frame
window(String nameOrHandle)WebDriverSwitch to window
alert()AlertSwitch to alert popup
activeElement()WebElementSwitch to active element

๐Ÿ”น 5. Alert Interface (JavaScript Alerts)

MethodReturn TypeDescription
accept()voidAccepts alert
dismiss()voidDismisses alert
getText()StringGets alert text
sendKeys(String keysToSend)voidSends input to alert

๐Ÿ”น 6. Select Class (Dropdowns)

MethodReturn TypeDescription
selectByVisibleText(String text)voidSelect by visible option text
selectByValue(String value)voidSelect by value attribute
selectByIndex(int index)voidSelect by index
deselectAll()voidDeselects all options
deselectByIndex(int index)voidDeselect by index
deselectByValue(String value)voidDeselect by value
deselectByVisibleText(String text)voidDeselect by text
getAllSelectedOptions()List<WebElement>Gets all selected options
getFirstSelectedOption()WebElementGets first selected option
getOptions()List<WebElement>Gets all options in dropdown
isMultiple()booleanChecks if multi-select

๐Ÿ”น 7. Options Interface (from driver.manage())

MethodReturn TypeDescription
window()WindowReturns window interface
timeouts()TimeoutsReturns timeouts interface
deleteAllCookies()voidDeletes all cookies
addCookie(Cookie cookie)voidAdds a cookie
deleteCookieNamed(String name)voidDeletes cookie by name
getCookies()Set<Cookie>Gets all cookies
getCookieNamed(String name)CookieGets cookie by name

๐Ÿ”น 8. Timeouts Interface

MethodReturn TypeDescription
implicitlyWait(Duration duration)TimeoutsSets implicit wait
pageLoadTimeout(Duration duration)TimeoutsSets max time for page load
scriptTimeout(Duration duration)TimeoutsSets time for async script execution

๐Ÿ”น 9. Actions Class (Mouse + Keyboard)

MethodReturn TypeDescription
click()ActionsClicks current mouse location
click(WebElement element)ActionsClicks given element
doubleClick()ActionsDouble-clicks
contextClick()ActionsRight-click
moveToElement(WebElement element)ActionsMove to element
dragAndDrop(WebElement src, WebElement target)ActionsDrags from source to target
sendKeys(CharSequence keys)ActionsTypes keys
keyDown(Keys key)ActionsHolds down key
keyUp(Keys key)ActionsReleases key
build()ActionBuilds the action chain
perform()voidExecutes the action chain

More from this blog

Shreenibas Samal's blog

49 posts