Simplified Guide to Basic Selenium WebDriver Commands
In this article, we'll explore some fundamental commands of Selenium WebDriver. These commands are essential for tasks such as opening websites, interacting with various web elements like buttons and text boxes, and managing browser sessions. Whether you're practicing on a sample page or a dummy e-commerce site, these commands are crucial for automating web browsers effectively.
For setup related doubts, refer to the 3rd Edition of TheTestSmith Automation Series.
Key Selenium WebDriver Commands
(1) Opening a Website
(1a) Using the Get Method: To open a website, you can use the driver.get() command with the website's URL as a parameter. For example:
driver.get("https://iam.thetestsmith.com");
(1b) Using the Navigate Method: Similarly, the driver.navigate().to() command also directs the browser to a specified URL:
driver.navigate().to("https://iam.thetestsmith.com");
(2) Interacting with Web Elements
(2a) Clicking on Elements: To click on a web element like a button, use the click() method. First, locate the element using an appropriate method such as By.id:
WebElement button = driver.findElement(By.id("buttonID"));
button.click();
(2b) Entering Text in a Textbox: Use the sendKeys() method to type text into a text input field:
WebElement textBox = driver.findElement(By.name("textBoxName"));
textBox.sendKeys("Your Text Here");
(2c) Clearing Text: To clear text from an input field, use the clear() method:
textBox.clear();
(3) Reading Text from Web Elements
To retrieve and use text from a web element, apply the getText() method:
领英推荐
String elementText = driver.findElement(By.id("elementID")).getText();
(4) Browser Navigation
(4a) Going Backward: Navigate back in the browser's history with navigate().back():
driver.navigate().back();
(4b) Going Forward: Move forward with navigate().forward():
driver.navigate().forward();
(4c) Refreshing the Page: Refresh the current webpage using navigate().refresh() or by re-entering the URL:
driver.navigate().refresh();
(5) Closing the Browser
(5a) Close Current Window: Use driver.close() to close the current browser window.
driver.close()
(5b) Close All Windows: To close all open browser windows, use driver.quit().
driver.quit()
Example Script
Here’s a simple script to demonstrate the use of these commands in automating a webpage:
// Set up the WebDriver and navigate to a sample site
System.setProperty("webdriver.chrome.driver", "path_to_driver");
WebDriver driver = new ChromeDriver();
driver.get("https://iam.thetestsmith.com");
// Perform various actions like clicking links, entering text, and navigating
WebElement link = driver.findElement(By.linkText("Sample Link"));
link.click();
WebElement inputField = driver.findElement(By.id("sampleInput"));
inputField.sendKeys("Test");
inputField.clear();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
// Close the browser after automation
driver.close();
Conclusion
Understanding these basic commands in Selenium WebDriver is crucial for anyone starting with web automation. By mastering these commands, you can automate many routine web interactions, making your testing process more efficient and effective. Happy testing!