Selenium Webdriver can be combined with the Expectedcondition class to define their own desired conditions
To create a new expectedcondition interface, you must implement the Apply method
Wait for the element to appear
1 Public voidtestwithimplicitwait () {2System.setproperty ("Webdriver.chrome.driver", "Chromedriver.exe");3Webdriver Driver =Newchromedriver ();4Driver.get ("http://map.baidu.com");5 6 //Click to expand the current city7Webelement curcity = driver.findelement (By.id ("Curcity"));8 Curcity.click ();9 Ten //set wait time 10 seconds Onewebdriverwait wait =NewWebdriverwait (driver,10); A //To create a new expecctedcondition interface, you must implement the Apply method -Webelement message =Wait.until ( - NewExpectedcondition<webelement>(){ the Publicwebelement Apply (webdriver d) { - returnD.findelement (By.id ("Selcityhotcityid")); - } - } + ); - + driver.quit (); A}
Sample Code
Wait for element property value to change
property values for an element may change after an operation based on certain events. For example, a text box that cannot be entered becomes an input state. The custom wait can be implemented on the attributes of the element.
In this example, the Expectedcondition class waits to return a Boolean value
1 (new webdriverwait (driver). Util (new expectedcondition<boolean>() { 2 Public Boolean Apply (webdriver d) {3 return d.findelement (by.id ("username")). 4 getattribute ("ReadOnly"). Contains ("true"); 5 }(6 }));
Wait for the element to become visible
The developer hides or displays an element after a series of actions. The specified element initially already exists in the DOM, but is hidden, and becomes visible when the user passes the specified action. Then the custom expectation criteria should be as follows:
1 (new webdriverwait (driver). Util (new expectedcondition<boolean>() { 2 Public Boolean Apply (webdriver d) {3 return d.findelement (by.id ("xxx")). isdisplayed (); 4 }(5 }));
Waiting for DOM events
A custom wait can be done by executing a piece of JavaScript code and checking the return value
1 (new webdriverwait (driver,10)). Until (new expectedcondition<boolean>() { 2 public Boolean apply (webdriver d) {3 javascriptexecutor js = ( Javascriptexecutor) D; 4 return (Boolean) Js.executescript ("return jquery.active = = 0"); 5 }6 });
[Selenium webdriver Java] using custom conditions to synchronize tests