Selenium UI Automation: Environment Setup, Element Locating Strategies, Wait Controls, and Common Operations
This article provides a practical guide to using Selenium for web UI automation, covering environment setup, seven element locating methods, time‑control techniques, window handling, headless mode, scrolling, cookie‑based login, screenshot capture, window closing, and page refresh with Java code examples.
Selenium is a tool for testing web applications and automating stable business UI processes. It works by using a browser‑specific WebDriver to interpret automation scripts and control the browser.
The article shares practical tips for quickly getting started with Selenium, especially for Java‑based UI automation.
1. How to set up the environment You need JDK, a browser, and the matching browser driver. Choose the appropriate driver version for the installed browser (Chrome, IE, Firefox, etc.).
2. Element locating methods Seven common strategies are demonstrated:
webDriver.findElement(By.name("phrase")); webDriver.findElement(By.linkText("发表")); webDriver.findElement(By.id("title")); webDriver.findElement(By.className("note-editable")); webDriver.findElement(By.cssSelector("li:nth-child(2)")).click(); webDriver.findElement(By.tagName("phrase")); driver.findElement(By.xpath("//img[@alt='div1-img1']"));3. Controlling time and effective element location Three wait mechanisms are described:
Thread.sleep(ms) – simple but inflexible.
Implicit wait: webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); – applies globally to all findElement calls.
Explicit wait – applied to a specific element, e.g., using WebDriverWait (illustrated with an image in the original).
4. Determining if an element exists Two approaches: catching the NoSuchElementException with try‑catch, or using Selenium’s built‑in API methods (shown in screenshots).
5. Switching windows When a new window opens, the WebDriver instance changes; use driver.switchTo().window(handle) to switch back (example for two windows provided).
6. Running in headless mode For server‑side execution on CentOS, enable headless mode for Chrome or Firefox, e.g.:
chromeOptions.addArguments("headless");7. Scrolling the page Execute JavaScript to scroll, for example:
((JavascriptExecutor) webDriver).executeScript("window.scrollBy(0, 7000)");8. Login without captcha Instead of solving captchas, reuse cookies from an already logged‑in session (illustrated with a diagram).
9. Capturing full‑page screenshots Use Selenium’s screenshot capabilities to capture the entire page (example shown).
10. Closing windows Two options: webdriver.close() to close the current window, or webdriver.quit() to quit the driver and close all associated windows.
11. Refreshing the page Multiple ways to refresh, such as:
driver.navigate().refresh(); driver.get(driver.getCurrentUrl()); driver.navigate().to(driver.getCurrentUrl()); driver.executeScript("history.go(0)");The article concludes with a brief promotion of Qtest, a professional testing team under 360.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.