When we perform selenium.type() or selenium.click() operations on the page, we often need to wait for an element to appear. For general web pages, when we enter a new page, we often use selenium.waitForPageToLoad(WAITTIME);
For elements in pop-up windows, you often write how many seconds you have to wait. In fact, you can use the following more general and efficient method, and write a waitForElement() method yourself:
Java code
protected void waitForElement(String target) { for (int second = 0;; second++) { if (second >= 60) { LOGGER.debug("Element:" + target + " can't be found after 60 seconds"); fail("find element timeout: " + target); } try { if (selenium.isElementPresent(target)) break; } catch (Exception e) { LOGGER.debug("Element:" + target + " can't be found in 60 seconds"); assert (false); } pause(1000); } }The function of this code is to let us wait for an element at most 60 seconds until it appears, otherwise the test case will fail. When we want to click or type an element, we will call this method first. The following is the improved click method
Java code
protected void click(String locator) { waitForElement(locator); selenium.click(locator); }From then on, we can regardless of whether the page is ajax implementation or not. As long as we call such a click() method, the code like Thread.sleep(10000) is not needed in the program.
The above example explanation of selenium's efficient response to web page element refresh is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.