实际测试中,不可避免的需要等待控件完全ready,才能进行相关的操作,这个时候,就需要动态的以小步递进的方式,对某个条件判断,进行时间等待,而不是简单的等待几秒。可以把相关函数封装在一个SeleniumUtils类中,供大家调用: import com.thoughtworks.selenium.Selenium; import com.thoughtworks.selenium.SeleneseTestCase; public class SeleniumUtils extends SeleneseTestCase { private Selenium selenium; public SeleniumUtils(Selenium selenium){ this.selenium=selenium; } public Selenium getSelenium() { return selenium; } public void pause(int milliSec){ try{ Thread.sleep(milliSec); }catch(InterruptedException e){ ; } } public boolean waitForElementEditable(String locator, int seconds){ boolean elementEditable=false; for(int second=0;;second++){ if(second>seconds){ break; } try{ if(selenium.isEditable(locator)){ elementEditable=true; break; } }catch(Exception e){ ; } pause(1000); } return elementEditable; } public boolean waitForElementPresent(String locator, int seconds){ boolean elementPresent=false; for(int second=0;;second++){ if(second>seconds){ break; } try{ if(selenium.isElementPresent(locator)){ elementPresent=true; break; } }catch(Exception e){ ; } pause(1000); } return elementPresent; } public boolean waitForTextPresent(String locator, int seconds){ boolean textPresent=false; for(int second=0;;second++){ if(second>seconds){ break; } try{ if(selenium.isTextPresent(locator)){ textPresent=true; break; } }catch(Exception e){ ; } pause(1000); } return textPresent; } public void waitForPageToLoad(String mis){ boolean isLoaded=false; int count=0; do{ if(count++>3){ break; } try{ selenium.waitForPageToLoad(mis); isLoaded=true; }catch(Exception ex){ continue; } }while(!isLoaded); } }