1. Click action for Browser Alert pop-up that may be delayed for some reason
Public void Waitalertclick () {
webdriverwait wait = new webdriverwait (driver, 10);//Maximum 10s
try {
Alert alert = Wait.until (new expectedcondition<alert> () {
@Override
Public Alert Apply (Webdriver driver1) {
try {
return Driver.switchto (). alert ();//Get Alert Object
} catch (Noalertpresentexception | Unhandledalertexception ignored) {//exceptions can be captured according to the actual situation
return null;
}
}
});
alert.accept ();//Click OK here, you can do other things
} catch (NullPointerException e) {
System.out.println ("No Alert");//wait timeout, no pop-up window, Alert null is executed here
}
2. For the display state of elements with hidden attributes, wait for the page load element to be visible, and then proceed to the next step
public void Waiteleisdisplay (by) {//wait for elements to be visible, up to 15s.
webdriverwait wait = new webdriverwait (driver, 15);
try {
Wait.until (New expectedcondition<boolean> () {
@Override
Public Boolean apply (Webdriver driver1) {
try {
Webelement ele=driver.findelement (by);
return ele.isdisplayed ();
}catch (Nosuchelementexception e) {
return null;
}
}
});
}catch (TimeoutException e) {
System.out.println ("Wait for element display timeout timed out after seconds waiting for:" +by);
}
}
Common methods for Web Automation encapsulation (occasional updates)