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
- get(): Navigate to a URL
- navigate().to(): Navigate to a URL with additional options
- back(): Go back to the previous page
- forward(): Go forward to the next page
- refresh(): Refresh the current page
Interaction Commands
- click(): Click an element
- sendKeys(): Send keys to an element
- clear(): Clear an element’s text
- submit(): Submit a form
- select(): Select an option from a dropdown
Verification Commands
- getTitle(): Get the page title
- getText(): Get the text of an element
- getAttribute(): Get an attribute of an element
- isDisplayed(): Check if an element is displayed
- isEnabled(): Check if an element is enabled
Wait Commands
- implicitlyWait(): Wait for an element to be present
- explicitlyWait(): Wait for a condition to be met
- 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.