Operations Based on page element locating fail automatically and page element locating
 
The web automation script fails to run. A major part of the problem lies in element locating. Here, the blogger introduces a method for loop locating and failure. To facilitate script maintenance and locate problems.
 
1. the following code is a WebDriver built-in method. encapsulate it and use it.
 
1/** 2 * capture the screen and save it to the specified path 3*4 * @ param filepath 5 * Save the full screen file name and Path 6 * @ return No 7 */8 public void captureScreenshot (String filepath) {9 File screenShotFile = (TakesScreenshot) driver) 10. getScreenshotAs (OutputType. FILE); 11 try {12 FileUtils. copyFile (screenShotFile, new File (filepath); 13} catch (Exception e) {14 e. printStackTrace (); 15} 16} 
2. Generate the storage path and file name in the public method, and print the failure information. You must manually create a directory file.
 
/*** Public method: generate the storage path and file name, and print the failure information ** @ param isSucceed * if your operation success */public void operationCheck (String methodName, boolean isSucceed) throws Exception {if (! IsSucceed) {Date date = new Date (); DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HHmmss"); String time = format. format (date); StringBuffer sb = new StringBuffer (); String captureName = sb. append (". /Captures /"). append (methodName ). append (time ). append (". png "). toString (); captureScreenshot (captureName); System. out. println ("method [" + methodName + "] failed to run. Please refer to the snapshot:" + captureName );}} 
3. During the specified time-out period, the page element is cyclically located. If the page element is successfully located, true is returned at any time. If no element is located within the time-out period, the method is executed and false is returned.
 
1/*** wait for the specified element appears with timeout setting. 3*4 * @ param locator 5 * the element locator on the page 6 * @ param timeout 7 * timeout time. Unit: 8 seconds */9 public boolean isElementPresent (By locator, long timeout) throws Exception {10 boolean isSucceed = false; 11 long timeBegins = System. currentTimeMillis (); 12 do {13 try {14 driver. findElement (locator); 15 isSucceed = true; 16 break; 17} catch (Exception e) {18 e. printStackTrace (); 19} 20 21} while (System. currentTimeMillis ()-timeBegins <= timeout * 1000); 22 23 operationCheck ("isElementPresent", isSucceed); 24 return isSucceed; 25} 
4. Common Methods for locating page elements. The isElementPresent method is called for each positioning element. Solve the click problem when the element is not loaded.
 
1/*** locate the page element and judge whether the element appears repeatedly during the timeout period. If element locating fails, 3*4 * @ param by 5 * Page element 6 * @ param timeout 7 * timeout time 8 * @ return page Element 9 */10 public WebElement getElement (By, long timeout) {11 try {12 if (isElementPresent (by, timeout) {13 return driver. findElement (by); 14} 15} catch (Exception e) {16 System. out. println (e + ""); 17} 18 return null; 19} 
5. The default timeout time is 5 seconds. You can directly call getElement (By by) when using the script.
 
1/*** locate the page element and judge whether the element appears repeatedly during the timeout period. If the element fails to be located, the 3*4 * @ param by 5 * Page Element
6 * @ return page element 7 */8 public WebElement getElement (By by) {9 return getElement (by, 5); 10}