My CoursesCorporate TrainingHire From Us Explore Courses

All courses

webinar

Selenium WebDriver Commands with Complete Examples

Selenium WebDriver’s command set is vast, but which ones do you really need to know? In this article, we’ll cut through the noise and focus on the essential commands that will take your automated testing to the next level

Selenium Commands with Explanation

Selenium WebDriver is a powerful tool for automating web browsers, allowing you to write test scripts in various programming languages. In this article, we’ll explore the top Selenium WebDriver commands, including navigation, interaction, verification, and more.

Navigation Commands

  1. get(): Navigate to a URL
  2. navigate().to(): Navigate to a URL with additional options
  3. back(): Go back to the previous page
  4. forward(): Go forward to the next page
  5. refresh(): Refresh the current page

Interaction Commands

  1. click(): Click an element
  2. sendKeys(): Send keys to an element
  3. clear(): Clear an element’s text
  4. submit(): Submit a form
  5. select(): Select an option from a dropdown

Verification Commands

  1. getTitle(): Get the page title
  2. getText(): Get the text of an element
  3. getAttribute(): Get an attribute of an element
  4. isDisplayed(): Check if an element is displayed
  5. isEnabled(): Check if an element is enabled

Wait Commands

  1. implicitlyWait(): Wait for an element to be present
  2. explicitlyWait(): Wait for a condition to be met
  3. pause(): Pause the execution for a specified time

Selenium WebDriver Commands with Examples

Fetching a web page

driver.get(“(link unavailable)”)

This command navigates to the specified URL and loads the web page. It’s like typing a URL in the browser’s address bar and pressing Enter.

Locating forms and sending user inputs

driver.findElement(By.name(“username”)).sendKeys(“johnDoe”)

This command finds a form element (e.g., a text box) with the name “username” and sends the text “johnDoe” to it. It’s like filling out a form on a web page.

Clearing User inputs

driver.findElement(By.name(“username”)).clear()

This command clears the text from a form element (e.g., a text box) with the name “username”. It’s like deleting text from a form field.

Fetching data over any web element 

driver.findElement(By.tagName(“h1”)).getText()

This command finds a web element (e.g., a heading) with the tag name “h1” and retrieves its text content. It’s like inspecting an element on a web page to see its text.

Performing Click event

driver.findElement(By.name(“submit”)).click()

This command finds a form element (e.g., a button) with the name “submit” and simulates a click on it. It’s like clicking a button on a web page.

Navigating backward in browser history

driver.navigate().back()

This command navigates to the previous page in the browser’s history. It’s like clicking the “Back” button in the browser.

Navigating forward in browser history

driver.navigate().forward()

This command navigates to the next page in the browser’s history. It’s like clicking the “Forward” button in the browser.

 

Refresh/ Reload a web page

driver.navigate().refresh()

This command reloads the current web page. It’s like clicking the “Reload” button in the browser.

Closing Browser

driver.close()

This command closes the current browser window. It’s like clicking the “Close” button on the browser window.

Closing Browser and other all other windows associated with the driver

driver.quit()

This command closes all browser windows associated with the driver and ends the session. It’s like quitting the browser application.

Moving between Windows 

driver.switchTo().window(“windowHandle”)

This command switches the focus to a different browser window with the specified handle. It’s like switching between multiple browser windows.

Moving between Frames

driver.switchTo().frame(“frameName”)

This command switches the focus to a different frame within the current browser window. It’s like switching between multiple frames on a web page.

Drag and Drop

driver.dragAndDrop(source, target)

This command simulates a drag-and-drop action from the source element to the target element. It’s like dragging an element and dropping it onto another element on a web page.

Conclusion

Mastering Selenium’s top commands is crucial for efficient web automation. By understanding and utilizing these commands, you can take your testing to the next level and ensure robust and reliable results.

FAQs

What are the basic Selenium commands?

Selenium commands like `click()`, `sendKeys()`, and `getText()` are fundamental for interacting with web elements. You can learn how to use these commands in our Selenium Training.

What is the use of `sendKeys()` in Selenium?

The `sendKeys()` command allows you to simulate typing text into input fields on web pages. Explore more about Selenium commands in our course guide.

What frameworks are commonly used with Selenium?

Common frameworks used with Selenium include TestNG, JUnit, and Cucumber. To understand how these frameworks integrate with Selenium, check out our Selenium course or visit our homepage.

What is TestNG in Selenium?

TestNG is a testing framework for Java that provides support for multiple test cases and data-driven testing. Learn more about TestNG integration in Selenium with our Selenium course.

How does Selenium integrate with Cucumber?

Selenium can be integrated with Cucumber for behavior-driven development (BDD), allowing you to write test scripts in plain English. Discover how to use Cucumber with Selenium in our training program.

What is the best framework for Selenium?

There is no one-size-fits-all answer, but popular frameworks like TestNG and Cucumber are highly recommended based on project needs. For a detailed guide on choosing the right framework, visit our Selenium course or contact us for advice.

Course Schedule

Name Date Details
Big Data Course 15 Jun 2024(Sat-Sun) Weekend Batch
View Details
Big Data Course 22 Jun 2024(Sat-Sun) Weekend Batch
View Details
Big Data Course 29 Jun 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Swathi
Automation Specialist (RPA & Testing)

Swathi is an experienced Automation Specialist with expertise in both Robotic Process Automation (RPA) and testing. She excels in designing and implementing automated workflows to streamline business processes, while also creating and executing test cases for software quality assurance. Swathi is proficient in integrating automation into CI/CD pipelines and enhancing efficiency through robust RPA solutions, ensuring seamless and high-quality project deliveries.