DEV Community

Code Green
Code Green

Posted on

Selenium webDriver Cheat sheet

Selenium WebDriver Cheat Sheet

Method Syntax Description
Initialize WebDriver WebDriver driver = new ChromeDriver(); Initializes the WebDriver for the specified browser.
Launch URL driver.get("http://www.example.com"); Navigates to the specified URL.
Find Element WebElement element = driver.findElement(By.id("id")); Locates a single WebElement using a specified selector.
Find Elements List<WebElement> elements = driver.findElements(By.className("class")); Locates multiple WebElements.
Click Element element.click(); Performs a click action on the specified WebElement.
Send Keys element.sendKeys("text"); Types text into an input field.
Get Element Text String text = element.getText(); Retrieves the visible text of a WebElement.
Get Element Attribute String value = element.getAttribute("attributeName"); Gets the value of a specified attribute of the element.
Get Page Title String title = driver.getTitle(); Retrieves the title of the current page.
Get Current URL String url = driver.getCurrentUrl(); Retrieves the URL of the current page.
Navigate Back driver.navigate().back(); Navigates back to the previous page in the browser history.
Navigate Forward driver.navigate().forward(); Navigates forward to the next page in the browser history.
Refresh Page driver.navigate().refresh(); Refreshes the current page.
Manage Cookies driver.manage().addCookie(new Cookie("name", "value")); Adds a cookie to the current session.
Delete All Cookies driver.manage().deleteAllCookies(); Deletes all cookies in the current session.
Maximize Window driver.manage().window().maximize(); Maximize the browser window.
Set Window Size driver.manage().window().setSize(new Dimension(width, height)); Set the size of the browser window.
Switch to Frame driver.switchTo().frame("frameName"); Switches the WebDriver context to the specified frame.
Switch to Alert Alert alert = driver.switchTo().alert(); Switches to an alert box.
Accept Alert alert.accept(); Accepts the currently displayed alert.
Dismiss Alert alert.dismiss(); Dismisses the currently displayed alert.
Quit Driver driver.quit(); Closes all associated windows and terminates the WebDriver session.

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺

Top comments (0)

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay