Selenium Commands List: The Ultimate Reference Guide
1
When you visit a website, you click buttons, type in search bars, or fill out forms. But have you ever thought about how testers make sure all these features work smoothly? Imagine testing an entire website manually every time a small change is made—this would take forever and still be prone to human errors! This is where automation testing comes in. With tools like Selenium WebDriver, testers can automate interactions on a webpage, making testing faster, more efficient, and highly accurate. However, just running test scripts isn’t enough. The key to strong, reliable tests lies in mastering Selenium commands, WebDriver methods, and waits in Selenium. If you've ever faced issues where tests fail unexpectedly, elements don’t load on time, or scripts run too fast for the page to catch up, then this guide is for you. We’ll dive deep into Selenium WebDriver commands, explore effective ways to manage waits, and share simple yet powerful tips to build robust automation scripts.
2
Understanding WebDriver Commands: The Backbone of Selenium Think of WebDriver commands as instructions given to a web browser to mimic human actions. These commands enable you to open web pages, click buttons, type text, retrieve data, and handle pop-ups—all without manual effort. By learning these commands, you gain complete control over the browser’s behavior, making test automation seamless and reliable. Let’s explore some of the most commonly used Selenium commands and how they function in real-world automation testing.
1. Browser Navigation Commands Imagine you're navigating a website as a normal user—clicking links, moving back and forth between pages, or refreshing the site. Selenium WebDriver commands allow you to replicate these actions programmatically. ● driver.get("URL") – Opens a webpage. This is the first step in nearly every Selenium test, as you need to load the page before interacting with elements. ● driver.navigate().to("URL") – Similar to get(), but provides additional navigation features. ● driver.navigate().back() – Simulates pressing the back button in a browser. ● driver.navigate().forward() – Moves forward if a page exists in the history.
3
● driver.navigate().refresh() – Reloads the current webpage, useful for testing dynamic content.
2. Web Element Interaction Commands Once a page loads, the next step is interacting with elements like buttons, text fields, and links. These commands allow testers to simulate user interactions efficiently. ● driver.findElement(By.locator()).click() – Clicks a button or link, just like a user would. ● driver.findElement(By.locator()).sendKeys("text") – Types text into an input field (such as login forms or search bars). ● driver.findElement(By.locator()).clear() – Clears any pre-filled text inside an input field. ● driver.findElement(By.locator()).getText() – Retrieves and verifies text from an element, useful for validating expected results.
3. Handling Alerts and Pop-ups Pop-ups and alerts often interrupt user actions. Selenium provides simple commands to handle them efficiently: ● driver.switchTo().alert().accept() – Clicks “OK” on an alert. ● driver.switchTo().alert().dismiss() – Clicks “Cancel” or closes an alert. ● driver.switchTo().alert().getText() – Retrieves the message displayed in the alert box. ● driver.switchTo().alert().sendKeys("input") – Allows users to enter text inside an alert box if needed.
4
4. Handling Frames and Windows Some web applications use iframes (embedded pages inside a page) or open multiple browser windows. Selenium provides commands to switch between them seamlessly. ● driver.switchTo().frame("frameName") – Moves control to an iframe. ● driver.switchTo().defaultContent() – Returns control to the main webpage. ● driver.switchTo().window("windowHandle") – Switches control to a new browser window.
Mastering Smart Waits in Selenium One of the biggest challenges in automation testing is dealing with page load times and dynamic content. Sometimes, elements take longer to load, and if Selenium tries to interact with them too soon, the test will fail. Instead of using random delays, it’s smarter to use waits in Selenium to make the script pause only until the element is fully available.
1. Implicit Wait This tells Selenium to wait for a specified duration before throwing an error when an element is not immediately found. // Set implicit wait to 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
5
Best for elements that load at a consistent speed. Not ideal for unpredictable elements, as it applies a universal wait time.
2. Explicit Wait Explicit waits allow Selenium to wait for a specific condition before proceeding with the next command. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(" someId")));
Best when dealing with dynamic elements that may appear after a few seconds.
3. Fluent Wait Fluent waits provide greater flexibility, allowing you to set retry intervals and ignore errors. FluentWait wait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(10)) .pollingEvery(Duration.ofSeconds(2)) .ignoring(NoSuchElementException.class);
Best when elements appear inconsistently due to AJAX calls or slow loading.
6
Advanced Selenium Methods for Efficient Testing 1. JavaScript Executor for Complex Interactions Some web elements are tricky—normal Selenium commands might not work. JavaScript Executor helps by injecting JavaScript commands into the browser. JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].scrollIntoView();", element);
It is best for handling hidden elements and scrolling issues.
2. Taking Screenshots for Debugging Screenshots provide visual proof of test failures, making debugging easier. File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("screenshot.png"));
It is Useful for logging bugs and sharing test results.
3. Handling Dropdowns with Select Class Standard dropdowns require special handling in Selenium, which is done using the Select class. Select dropdown = new Select(driver.findElement(By.id("dropdownId"))); dropdown.selectByVisibleText("Option 1");
It ensures proper selection of dropdown values during automation.
7
Conclusion: Becoming a Selenium Pro Mastering Selenium WebDriver commands and smart waits is essential for writing stable automation scripts. If your tests keep failing because elements don’t load properly, using methods in Selenium like explicit and fluent waits can make a huge difference. Key takeaways: ● Use navigation and interaction commands to automate web applications efficiently. ● Implement smart waits to avoid element loading issues. ● Apply advanced Selenium techniques like JavaScript execution and screenshot capturing to improve performance. ● Keep your test cases organized and maintainable to scale automation efforts smoothly. By following these best practices, you can level up your Selenium skills and build flawless, reliable automation scripts that work consistently every time! Source: For more details , readers may refer to Devto.
8